Last active
July 2, 2020 15:32
-
-
Save kenzo0107/2941e3c5433603045c81c25dbc454afc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/go-playground/validator" | |
) | |
type User struct { | |
FirstName string `validate:"required"` //必須 | |
LastName string `validate:"required"` //必須 | |
Age uint8 `validate:"gte=0,lt=130"` // 0 <= age < 130 | |
Email string `validate:"required,email"` //必須, email フォーマット | |
} | |
func main() { | |
user := &User{ | |
FirstName: "Kenzo", | |
LastName: "Tanaka", | |
Age: 39, | |
Email: "kenzo.tanakagmail.com", | |
} | |
validate := validator.New() | |
error := validate.Struct(user) | |
fmt.Println(error) // Key: 'User.Email' Error:Field validation for 'Email' failed on the 'email' tag | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment