https:\/\/github.com\/asaskevich\/govalidator<\/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 Name string\n}<\/code><\/pre>\n\n\n\n<\/span>First Library (go-playground\/validator)<\/strong><\/span><\/h1>\n\n\n\nLet’s first see the playground validator library. Below is the code for the same.<\/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)\n\nvar validate *validator.Validate\n\ntype employee struct {\n Name string `validate:\"required\"`\n}\n\nfunc main() {\n e := employee{}\n err := validateStruct(e)\n if err != nil {\n fmt.Printf(\"Error: %s\\n\", err)\n }\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.Name' Error:Field validation for 'Name' failed on the 'required' 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 Name field. This tag is interpreted by the playground validate library.<\/p>\n\n\n\n
type employee struct {\n Name string `validate:\"required\"`\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\nIt raises the correct error as we have passed the Name<\/strong> field as nil<\/p>\n\n\n\n<\/span>Second Library (asaskevich\/govalidator)<\/strong><\/span><\/h1>\n\n\n\ngo.mod<\/strong><\/p>\n\n\n\nmodule sample.com\/validator\ngo 1.14\n\nrequire github.com\/asaskevich\/govalidator v0.0.0-20200907205600-7a23bdc65eef<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com\/asaskevich\/govalidator\"\n)\n\ntype employee struct {\n\tName string `valid:\"required\"`\n}\n\nfunc main() {\n\te := employee{}\n\terr := validateStruct(e)\n\tif err != nil {\n\t\tfmt.Printf(\"Error: %s\\n\", err)\n\t}\n}\n\nfunc validateStruct(e employee) error {\n\t_, err := govalidator.ValidateStruct(e)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nError: Name: non zero value required<\/code><\/pre>\n\n\n\nSimilar to the above example we associated tags with the Name field which govalidator can interpret<\/p>\n\n\n\n
Name string `valid:\"required\"`<\/code><\/pre>\n\n\n\nThen we call the ValidateStruct<\/strong> function and it raises the correct error as we have passed the Name<\/strong> field as nil<\/p>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewFirst Library (go-playground\/validator)Second Library (asaskevich\/govalidator) Overview In this tutorial, we will explore two libraries that can be used to validate the field of a struct in Golang. The two…<\/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":[3,4,312],"class_list":["post-5157","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang","tag-validation"],"yoast_head":"\n
Validate the presence of the field 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