我想在 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"`
}
您可以使用常规引号。您只需要转义更多字符,尤其是 struct 标记的 value 部分周围的引号。
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}