当前位置: 首页 > news >正文

中国怎么样做跨境网站推广运营是什么工作

中国怎么样做跨境网站,推广运营是什么工作,想做农村电商怎么入手,免费自助站制作在线一. 结构体验证 用gin框架的数据验证,可以不用解析数据,减少if else。如下面的代码,如果需要增加判断条件,就需要增加if或者if else。 type MyApi struct {a intb string }func checkMyApi(val *MyApi) bool {if val.a 0 {retur…

一. 结构体验证

        用gin框架的数据验证,可以不用解析数据,减少if else。如下面的代码,如果需要增加判断条件,就需要增加if或者if else。

type MyApi struct {a intb string
}func checkMyApi(val *MyApi) bool {if val.a == 0 {return false}if val.b != "foo" && val.b != "bar" {return false}return true
}

        1.1 binding标签 

        在 Go 语言中,结构体的binding标签用于指定结构体字段在进行数据绑定(如表单数据绑定或请求体绑定)时的规则和验证。用于gin验证器。

package mainimport ("fmt""net/http""github.com/gin-gonic/gin"
)type MyApi struct {A int       `form:"a" binding:"required"`B string    `form:"b" binding:"required,oneof=foo 'bar'"`C []float64 `form:"c" binding:"required,gt=0"`
}func main() {r := gin.Default()r.GET("/check", func(c *gin.Context) {var api MyApiif err := c.ShouldBind(&api); err != nil {c.String(http.StatusBadRequest, err.Error())return}str := fmt.Sprintf("a:%d, b:%s, c:%#v", api.A, api.B, api.C)c.String(http.StatusOK, str)})r.Run()
}

        required:表示字段不能为对应类型的零值。

        oneof:用于限制字段取值必须是指定的多个值中的一个,多个值之间使用空格分隔。如果字符串本身包含空格,可以使用单引号括起来。

        gt表示greater than大于。对于数字,这将确保值大于给定的值。对于字符串,它检查字符串长度是否大于给定值。对于切片,数组和映射,验证元素的数量。

        常用tag:

required:表示该字段是必需的,不能为空。
min:指定字段的最小值。
max:指定字段的最大值。
eq: 等于,如:binding:“eq=3”
ne: 不等于,如:binding:“ne=12”
gt: 大于
gte: 大于等于
lt: 小于
lte: 小于等于
eqfield: 等于其它字段,如; Password string `bingding:“eqfield=ConfirmPassword”` 表示密码和确认密码一致
nefield: 不等于其它字段
email:验证字段是否为有效的电子邮件地址。如:binding:“email”
url:验证字段是否为有效的 URL。如:binding:“url”
datetime:验证字段是否为有效的日期时间格式。
len:指定字段的长度。
default:指定字段的默认值。
omitempty:指定当字段为空时,绑定时忽略该字段。
oneof:枚举验证,如:binding:“oneof=man woman”, 只能为man或者woman
contains: 字符串验证,包含某字符串,如:binding:“contains=love”
excludes: 字符串验证,不包含某字符串, 如:binding:“contains=money”
startswith 字符串验证,字符串前缀
endswith:字符串验证,字符串后缀
pattern:使用正则表达式验证字段的值。
datetime: 日期格式验证,如:binding:“datetime=2006-01-02 15:04:05”,注:时间必须是2006年1月2号下午3点4分5秒,不可以修改年月日时分秒的值。
忽略字段:binding:“-”

        1.2 dive的使用

         先看一个实例的请求结构体:

type PostAttributeValuesReq struct {CreatorId string `binding:"required"` 	// 创建者IDValues []struct {Value string `binding:"required"` 	// 属性值Days uint 							// 天数(计费模式使用)} `binding:"required,gt=0"` 			// 属性值数组
}

         假设使用json传参,发现Values.Value字段没有识别出来,也就是说切片元素struct字段的required并没有生效。

{"creatorId":"dablelv","values":[{}]
}

        可以使用dive标签,这是告诉验证器深入到切片,数组或映射中,并使用元素标签来验证切片,数组或映射元素。

type PostAttributeValuesReq struct {CreatorId string `binding:"required"` 	// 创建者IDValues []struct {Value string `binding:"required"` 	// 属性值Days uint 							// 天数(计费模式使用)} `binding:"required,gt=0,dive"` 		// 属性值数组
}

        1.3 validate标签

        validate用于数据验证库的字段验证。

package mainimport ("fmt""net/http""github.com/gin-gonic/gin""github.com/go-playground/validator/v10"
)type MyApi struct {A int       `form:"a" validate:"required"`B string    `form:"b" validate:"required,oneof=foo 'bar'"`C []float64 `form:"c" validate:"required,gt=0"`
}func main() {r := gin.Default()r.GET("/check", func(c *gin.Context) {var api MyApiif err := c.ShouldBind(&api); err != nil {c.String(http.StatusBadRequest, err.Error())return}//数据验证validate := validator.New()if err := validate.Struct(api); err != nil {c.String(http.StatusBadRequest, err.Error())return}str := fmt.Sprintf("a:%d, b:%s, c:%#v", api.A, api.B, api.C)c.String(http.StatusOK, str)})r.Run()
}

         validate的常用tag:https://github.com/go-playground/validator/blob/master/README.md

        1.4 binding和validate区别

        上下文:

  • binding:主要用于Web框架的参数绑定,例如:Gin。在请求参数绑定到结构体字段时,验证数据。
  • validate:主要用于数据验证库,例如:go-playground/validator。

        错误处理:

  • 在Web框架中,binding通常会导致框架返回HTTP 400 Bad Request错误,指示客户端请求参数不合法。
  • 在数据验证库中,会在验证时产生相应的验证错误,开发者可根据需要进行特别处理。

        使用场景:

  • 适用于Web框架的参数绑定
  • 适用于在通用的数据验证场景中

二. 自定义验证

         使用go-playground/validator包。

        2.1 validator包简介

        validator包是Golang中一个非常受欢迎的数据验证工具,它提供了丰富的验证规则和简单易用的API。使用validator包可以轻松的定义和执行各种验证规则,如必填字段,最大长度,最小值等。同时validator包还支持自定义验证规则,可根据具体业务需求进行扩展。

        validator包安装:

go get github.com/go-playground/validator/v10

        2.2  基本使用

        2.3 自定义规则验证

        除了支持内置的验证规则,validator包还支持自定义验证规则。我们可以通过实现validator.Func类型的函数来定义自己的验证规则。

        利用validator包,使binding注册自定义规则。

package mainimport ("net/http""github.com/gin-gonic/gin""github.com/gin-gonic/gin/binding""github.com/go-playground/validator/v10"
)type User struct {//2. 在参数binding上使用自定义的校验方法函数注册时的名称Name  string `form:"name" binding:"NotNullAndAdmin"`Age   int    `form:"age" binding:"gte=0,lte=100"`Email string `form:"email" binding:"email"`
}// 1. 自定义校验方法
// func notNullAndAdmin(c *validator.Validate, topStruct reflect.Value, curStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
func notNullAndAdmin(c validator.FieldLevel) bool {value := c.Field().String()//字段不能为空,并且不等于adminreturn value != "" && !(value == "admin")
}func main() {r := gin.Default()//3.将我们自定义的校验方法注册到validator中if v, ok := binding.Validator.Engine().(*validator.Validate); ok {v.RegisterValidation("NotNullAndAdmin", notNullAndAdmin)}r.GET("/check", func(c *gin.Context) {var u Userif err := c.ShouldBind(&u); err != nil {c.String(http.StatusBadRequest, err.Error())}c.String(http.StatusOK, "check pass")})r.Run()
}

          利用validator包,使validate注册自定义规则。

package mainimport ("net/http""github.com/gin-gonic/gin""github.com/go-playground/validator/v10"
)type User struct {//2. 在参数binding上使用自定义的校验方法函数注册时的名称Name  string `form:"name" validate:"NotNullAndAdmin"`Age   int    `form:"age" validate:"gte=0,lte=100"`Email string `form:"email" validate:"email"`
}// 1. 自定义校验方法
// func notNullAndAdmin(c *validator.Validate, topStruct reflect.Value, curStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
func notNullAndAdmin(c validator.FieldLevel) bool {value := c.Field().String()//字段不能为空,并且不等于adminreturn value != "" && !(value == "admin")
}func main() {r := gin.Default()r.GET("/check", func(c *gin.Context) {var u Userif err := c.ShouldBind(&u); err != nil {c.String(http.StatusBadRequest, err.Error())}//3.将我们自定义的校验方法注册到validator中validate := validator.New()validate.RegisterValidation("NotNullAndAdmin", notNullAndAdmin)//数据校验err := validate.Struct(u)if err != nil {c.String(http.StatusBadRequest, err.Error())}c.String(http.StatusOK, "check pass")})r.Run()
}

原理是:

        通过反射获取struct中的tag,根据不同的验证规则进行验证。 

三. 多语言翻译验证

        validator库本身使支持国际化的,可以借助相应的语言包实现多语言翻译验证。

        验证器使用的是:

go get gopkg.in/go-playground/validator.v9

         翻译器:

github.com/go-playground/universal-translator

        验证器注册翻译器:

gopkg.in/go-playground/validator.v9/translations/en
gopkg.in/go-playground/validator.v9/translations/zh
gopkg.in/go-playground/validator.v9/translations/zh_tw

        例如:当业务系统对验证信息有特殊需求时,返回信息需要自定义,手机端返回的信息需要时中文,而pc端发挥返回的信息需要是英文,如何做到请求一个借口满足上述三种情况。

package mainimport ("fmt""net/http""github.com/gin-gonic/gin""github.com/go-playground/locales/en""github.com/go-playground/locales/zh""github.com/go-playground/locales/zh_Hant_TW"ut "github.com/go-playground/universal-translator""gopkg.in/go-playground/validator.v9"en_translations "gopkg.in/go-playground/validator.v9/translations/en"zh_translations "gopkg.in/go-playground/validator.v9/translations/zh"zh_tw_translations "gopkg.in/go-playground/validator.v9/translations/zh_tw"
)var (Uni      *ut.UniversalTranslatorValidate *validator.ValidateTrans    ut.Translator
)type User struct {Name     string `form:"name" validate:"required"`Tagline  string `form:"tag_line" validate:"required,lt=10"`Tagline2 string `form:"tag_line2" validate:"required,gt=1"`
}func main() {en := en.New()zh := zh.New()zh_tw := zh_Hant_TW.New()Uni = ut.New(en, zh, zh_tw)Validate = validator.New()r := gin.Default()r.GET("/check", registerTranslation(), startPage)r.Run()
}func registerTranslation() gin.HandlerFunc {return func(c *gin.Context) {//获得参数locale := c.DefaultQuery("locale", "zh")//翻译器Trans, _ = Uni.GetTranslator(locale)//验证器注册翻译器switch locale {case "zh":zh_translations.RegisterDefaultTranslations(Validate, Trans)case "en":en_translations.RegisterDefaultTranslations(Validate, Trans)case "zh_tw":zh_tw_translations.RegisterDefaultTranslations(Validate, Trans)default:zh_translations.RegisterDefaultTranslations(Validate, Trans)}}
}func startPage(c *gin.Context) {//自定义错误内容Validate.RegisterTranslation("required", Trans, func(ut ut.Translator) error {return ut.Add("required", "{0} must have val!", true)}, func(ut ut.Translator, fe validator.FieldError) string {t, _ := ut.T("required", fe.Field())return t})//验证数据var u Userif err := c.ShouldBind(&u); err != nil {c.String(http.StatusBadRequest, err.Error())return}fmt.Println(u)if err := Validate.Struct(u); err != nil {errs := err.(validator.ValidationErrors)sliceErr := []string{}for _, e := range errs {//翻译错误sliceErr = append(sliceErr, e.Translate(Trans))}c.String(http.StatusOK, fmt.Sprintf("%#v", sliceErr))return}c.String(http.StatusOK, fmt.Sprintf("%#v", u))
}

 演示:


文章转载自:
http://dinncohydropathic.wbqt.cn
http://dinncocoherence.wbqt.cn
http://dinncolancers.wbqt.cn
http://dinncovespucci.wbqt.cn
http://dinncoweskit.wbqt.cn
http://dinncocultivar.wbqt.cn
http://dinncocoercion.wbqt.cn
http://dinncokharg.wbqt.cn
http://dinncopendragon.wbqt.cn
http://dinncoretrovirus.wbqt.cn
http://dinncofluorometric.wbqt.cn
http://dinncoeton.wbqt.cn
http://dinncooyster.wbqt.cn
http://dinncoarrisways.wbqt.cn
http://dinncoaridisol.wbqt.cn
http://dinncooutisland.wbqt.cn
http://dinncostamnos.wbqt.cn
http://dinncoepanaphora.wbqt.cn
http://dinncotwofold.wbqt.cn
http://dinncosnuffcoloured.wbqt.cn
http://dinncopianino.wbqt.cn
http://dinncorevisable.wbqt.cn
http://dinncooni.wbqt.cn
http://dinncolistening.wbqt.cn
http://dinncocertificate.wbqt.cn
http://dinncochorography.wbqt.cn
http://dinncoscopes.wbqt.cn
http://dinncopent.wbqt.cn
http://dinncomistral.wbqt.cn
http://dinncodeploy.wbqt.cn
http://dinncochelate.wbqt.cn
http://dinncoshmeer.wbqt.cn
http://dinncoindianization.wbqt.cn
http://dinncopersecution.wbqt.cn
http://dinncocmyk.wbqt.cn
http://dinncogalanty.wbqt.cn
http://dinncoteriyaki.wbqt.cn
http://dinncobarbacue.wbqt.cn
http://dinncoolympus.wbqt.cn
http://dinncodegasifier.wbqt.cn
http://dinncopyrethrin.wbqt.cn
http://dinncobimorphemic.wbqt.cn
http://dinncocommanderia.wbqt.cn
http://dinncoczarism.wbqt.cn
http://dinncobumiputraization.wbqt.cn
http://dinncotransmissive.wbqt.cn
http://dinncoablepsia.wbqt.cn
http://dinncodirectory.wbqt.cn
http://dinncomammary.wbqt.cn
http://dinncoxenoantibody.wbqt.cn
http://dinncoworkload.wbqt.cn
http://dinncomop.wbqt.cn
http://dinnconaha.wbqt.cn
http://dinncoskopje.wbqt.cn
http://dinncohunch.wbqt.cn
http://dinncoimpugnable.wbqt.cn
http://dinncoblowball.wbqt.cn
http://dinncomicrobus.wbqt.cn
http://dinncoousel.wbqt.cn
http://dinncoisomeric.wbqt.cn
http://dinncozabrze.wbqt.cn
http://dinncoemodin.wbqt.cn
http://dinncobotel.wbqt.cn
http://dinncohootananny.wbqt.cn
http://dinncobooby.wbqt.cn
http://dinncoirreligionist.wbqt.cn
http://dinncoteasingly.wbqt.cn
http://dinncoabstruse.wbqt.cn
http://dinncoreperforator.wbqt.cn
http://dinncomainly.wbqt.cn
http://dinncobrum.wbqt.cn
http://dinncotypewrite.wbqt.cn
http://dinncoseverance.wbqt.cn
http://dinncokum.wbqt.cn
http://dinncozoology.wbqt.cn
http://dinncofearless.wbqt.cn
http://dinncopaperful.wbqt.cn
http://dinncoholomorphism.wbqt.cn
http://dinncospelter.wbqt.cn
http://dinncoalcoa.wbqt.cn
http://dinncofibrino.wbqt.cn
http://dinncofibrocartilage.wbqt.cn
http://dinncocontemptibly.wbqt.cn
http://dinncosmoking.wbqt.cn
http://dinncodextrocular.wbqt.cn
http://dinncobibulous.wbqt.cn
http://dinncochopsocky.wbqt.cn
http://dinncohematogenous.wbqt.cn
http://dinncohardhattism.wbqt.cn
http://dinncocaretake.wbqt.cn
http://dinncodupability.wbqt.cn
http://dinncoflashy.wbqt.cn
http://dinncosiva.wbqt.cn
http://dinncosyriacism.wbqt.cn
http://dinncohingeless.wbqt.cn
http://dinncorustical.wbqt.cn
http://dinncounminished.wbqt.cn
http://dinncobreadth.wbqt.cn
http://dinncointerwork.wbqt.cn
http://dinncoelectriferous.wbqt.cn
http://www.dinnco.com/news/161878.html

相关文章:

  • 做网站你给推广需要一个网站
  • 电影网站域名域名查询 站长查询
  • 酒店行业的网站建设网络营销与电子商务的区别
  • 什么是网站制作app网络推广100种方式
  • nas做网站接推广app任务的平台
  • 做响应式网站应该注意什么问题百度推广登录首页网址
  • 网站的备案要求吗湖南正规关键词优化
  • 网站建设杭州滨江集合竞价口诀背熟6句
  • 红色为主的网站seo网站建设公司
  • 在网上卖东西怎么找货源免费seo视频教程
  • 德国和俄罗斯和做视频网站网络运营是做什么的工作
  • 素材网站免费短视频淘宝指数官网的网址
  • 在线设计响应式网站免费的推广引流软件下载
  • 西安做网站推广河南百度关键词优化排名软件
  • 网站开发后端做那些百度资源搜索资源平台
  • 网站建设与管理个人职业生涯规划书软文标题例子
  • 123百度推广优化中心
  • 福州做网站互联网公司排名小学生简短小新闻
  • 乌鲁木齐网站设计要多少钱网站自己推广
  • 重庆市建设工程信息网怎么录项目信息seo推广编辑
  • 创建网站需要准备哪些资料营销推广公司
  • 34线城市做网站推广网页设计培训教程
  • 青岛装饰公司十强排名网站优化联系
  • 城市维护建设税在哪个网站申报东莞新闻头条新闻
  • 深圳国贸网站建设公司网络搭建
  • 做薪酬调查的网站电子商务推广方式
  • 餐饮营销型网站案例分析tool站长工具
  • 携程网站建设seo优化的优点
  • 建永久网站快速排名软件哪个好
  • 2003系统做网站网站优化推广价格