数据绑定与验证
下载安装
go get github.com/go-macaron/binding使用示例
获取表单数据
type ContactForm struct {
Name string `form:"name" binding:"Required"`
Email string `form:"email"`
Message string `form:"message" binding:"Required"`
MailingAddress string `form:"mailing_address"`
}m.Post("/contact/submit", binding.Bind(ContactForm{}), func(contact ContactForm) string {
return fmt.Sprintf("Name: %s\nEmail: %s\nMessage: %s\nMailing Address: %v",
contact.Name, contact.Email, contact.Message, contact.MailingAddress)
})命名约定
获取 JSON 数据
绑定到接口
处理器说明
Bind
Form
MultipartForm 和文件上传
使用示例
Json
Validate
验证规则
自定义操作
自定义验证
自定义验证规则
自定义错误处理
Last updated
Was this helpful?