我想在Go结构体标签内转义反引号。例如,在下面的代码中:
type User struct {
email string `validate: "regexp=`"`
password string `validate: "min=8"`
}
我想在Go结构体标签内转义反引号。例如,在下面的代码中:
type User struct {
email string `validate: "regexp=`"`
password string `validate: "min=8"`
}
您可以使用普通的引号,但是您需要转义更多字符,特别是结构标记值周围的引号。
type User struct {
Email string "validate:\"regexp=`\""
Password string `validate:"min=8"`
}
使用反射验证标签的值:
func main() {
s := reflect.ValueOf(&User{}).Elem()
fmt.Println(s.Type().Field(0))
}
输出:
{Email string validate:"regexp=`" 0 [0] false}
build command-line-arguments: cannot load gopkg.in/validator.v2: module gopkg.in/validator.v2: Get https://proxy.golang.org/gopkg.in/validator.v2/@v/list: unexpected EOF
。 - Victor Timofei