https:\/\/pkg.go.dev\/github.com\/go-playground\/validator<\/a><\/li><\/ul>\n\n\n\nFor this tutorial, we will use the below employee struct<\/p>\n\n\n\n
type employee struct {\n Age int\n}<\/code><\/pre>\n\n\n\n<\/span>Example<\/strong><\/span><\/h1>\n\n\n\nLet’s see an example for the same. Below is the code<\/p>\n\n\n\n
go.mod<\/strong><\/p>\n\n\n\nmodule sample.com\/validate\ngo 1.14\nrequire (\n github.com\/go-playground\/universal-translator v0.17.0 \/\/ indirect\n github.com\/leodido\/go-urn v1.2.1 \/\/ indirect\n gopkg.in\/go-playground\/assert.v1 v1.2.1 \/\/ indirect\n gopkg.in\/go-playground\/validator.v9 v9.31.0\n)<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\nimport (\n \"fmt\"\n \"gopkg.in\/go-playground\/validator.v9\"\n)\nvar validate *validator.Validate\ntype employee struct {\n Age int `validate:\"required,gte=10,lte=20\"`\n}\nfunc main() {\n e := employee{}\n err := validateStruct(e)\n if err != nil {\n fmt.Printf(\"Error: %s\\n\", err)\n }\n e = employee{Age: 5}\n err = validateStruct(e)\n if err != nil {\n fmt.Printf(\"Error: %s\\n\", err)\n }\n e = employee{Age: 25}\n err = validateStruct(e)\n if err != nil {\n fmt.Printf(\"Error: %s\\n\", err)\n }\n}\nfunc validateStruct(e employee) error {\n validate = validator.New()\n err := validate.Struct(e)\n if err != nil {\n return err\n }\n return nil\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nError: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'required' tag\nError: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'gte' tag\nError: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'lte' tag<\/code><\/pre>\n\n\n\nFirst, we need to declare the instance of Validate<\/p>\n\n\n\n
var validate *validator.Validate<\/code><\/pre>\n\n\n\nNotice here that we need to associate meta tags with fields of the struct to let the validator know that you want to validate this field. In the above example, we added the tag with the Age field. This tag is interpreted by the playground validate library. Notice we added three validations for the Age<\/strong> field<\/p>\n\n\n\nrequired<\/strong> – validates that the field is present<\/li><\/ul>\n\n\n\ngte<\/strong> – validate that the field value is greater than equal to a particular value<\/li><\/ul>\n\n\n\nlte<\/strong> – validate that the field value is less than equal to a particular value<\/li><\/ul>\n\n\n\ntype employee struct {\n\tAge int `validate:\"required,gte=10,lte=20\"`\n}<\/code><\/pre>\n\n\n\nThen call the Struct method to validate the struct<\/p>\n\n\n\n
validate.Struct(e)<\/code><\/pre>\n\n\n\nFor<\/p>\n\n\n\n
e := employee{}<\/code><\/pre>\n\n\n\nit gives the output as below is Age<\/strong> field is empty<\/p>\n\n\n\nError: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'required' tag<\/code><\/pre>\n\n\n\nFor<\/p>\n\n\n\n
e := employee{Age: 5}<\/code><\/pre>\n\n\n\nit gives the output as below as Age<\/strong> field value is 5 which is less than 10<\/p>\n\n\n\nError: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'gte' tag<\/code><\/pre>\n\n\n\nFor<\/p>\n\n\n\n
e := employee{Age: 25}<\/code><\/pre>\n\n\n\nit gives the output as below as Age<\/strong> field value is 25 which is greater than 20<\/p>\n\n\n\nError: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'lte' tag<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewExample Overview The below library can be used to validate the range of an integer in a struct in Golang gopkg.in\/go-playground\/validator.v9 – https:\/\/pkg.go.dev\/github.com\/go-playground\/validator For this tutorial, we will use…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[4,268,134,312],"class_list":["post-5162","post","type-post","status-publish","format-standard","hentry","category-tech","tag-golang","tag-gp","tag-range","tag-validation"],"yoast_head":"\n
Validate the range of the integer in a struct in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n