今天小编给大家分享一下go语言beego框架web开发语法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
第一个参数是请求路径,第二个参数是http状态码。
c.Redirect("/login",400) //重定向
c.TplName = "login.html"
设置主键 `pk`
设置自增 `auto`
注意:当Field类型为int,int32,int64,uint,uint32,uint64时,可以设置字段为自增健,当模型定义中没有主键时,符合上述类型且名称为Id的Field将视为自增健。
设置默认值 `default(1111)`
设置长长度 `orm:size(100)`
设置允许为空 `null`,数据库默认是非空,设置null后可变成`ALLOW NULL`
设置唯一 `orm:"unique"`
设置浮点数精度 `orm:"digits(12);decimals(4)"` //总共12位,四位是小数
设置时间 `orm:"auto_now_add;type(datetime)"`
`orm:"auto_now;type(date)"`
注意:
auto_now 每次model保存时都会对时间自动更新
auto_now_add 第一次保存时才设置时间
设置时间的格式:type
# 案例
type User struct {
beego.Controller
Id int `orm:"pk;auto"` //主键且自增
Name string `orm:"size(20)"` //长度20
CreateTime time.Time
Count int `orm:"defaule(0);null"` //默认为0,可以为空
}
c.GetString("userName") //获取字符串
func (c*MainController) AddAritcle() {
c.Data["name"] = c.GetString("userName")
c.Data["pwd"] = c.GetString("passwd")
beego.Info("用户名:",c.Data["name"])
beego.Info("密码",c.Data["pwd"])
c.TplName = "success.html"
}
f,h,err :=c.GetFile("file_name")
//获取文件
//f:文件句柄
//h:文件信息
//err:错误信息
defer f.Close()
if err != nil{
beego.Info("上传文件失败")
}else {
c.SaveToFile("file_name","./staic/img/"+h.Filename)
}
Html
就是别忘记在你的 form 表单中增加这个属性 enctype="multipart/form-data",否则你的浏览器不会传输你的上传文件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆</title>
</head>
<body>
<div>
<div >
<form action="/addAritcle" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="userName">
<p></p> 密码:<input type="password" name="passwd">
<input type="file" name="uploadfilename">
<p></p> <input type="submit" value="注册">
</form>
</div>
</div>
</body>
</html>
fileext := path.Ext(h.Filename)
var table_lis []models.User
_,err := o.QueryTable("User").All(&table_lis)
if err !=nil{
beego.Info("查询文章出错")
return
}
beego.Info(table_lis)
c.Data["table_lis"] = table_lis //业务逻辑传过来的值
{{range .table_lis}} //循环访问
<tr>
<td>{{.Name}}</td>
<td>{{.PassWord}}</td>
</tr>
{{end}}
<td>{{.time.Format "2006-01-02"}}</td> //格式化时间
<td><a href="/addAritcle?id={{.Id}}" rel="external nofollow" ></a></td>
以上就是“go语言beego框架web开发语法是什么”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。