我想在Go struct标记内转义。例如下面的代码:
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}