Skip to content

Instantly share code, notes, and snippets.

@kenzo0107
Last active July 2, 2020 15:32
Show Gist options
  • Save kenzo0107/2941e3c5433603045c81c25dbc454afc to your computer and use it in GitHub Desktop.
Save kenzo0107/2941e3c5433603045c81c25dbc454afc to your computer and use it in GitHub Desktop.
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