{"id":21577948,"url":"https://github.com/wesleysbmartins/go_custom_tags","last_synced_at":"2026-05-17T12:05:13.656Z","repository":{"id":250643389,"uuid":"835009424","full_name":"wesleysbmartins/go_custom_tags","owner":"wesleysbmartins","description":"Criação e validação de tags customizadas para structs em golang, usando o pacote nativo reflect.","archived":false,"fork":false,"pushed_at":"2024-07-29T02:20:00.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T07:28:22.768Z","etag":null,"topics":["customized-tags","fields","golang","reflect","struct","tags"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wesleysbmartins.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-29T01:04:20.000Z","updated_at":"2024-07-29T02:24:36.000Z","dependencies_parsed_at":"2024-07-29T04:56:57.844Z","dependency_job_id":null,"html_url":"https://github.com/wesleysbmartins/go_custom_tags","commit_stats":null,"previous_names":["wesleysbmartins/go_custom_tags"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/wesleysbmartins/go_custom_tags","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleysbmartins%2Fgo_custom_tags","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleysbmartins%2Fgo_custom_tags/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleysbmartins%2Fgo_custom_tags/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleysbmartins%2Fgo_custom_tags/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wesleysbmartins","download_url":"https://codeload.github.com/wesleysbmartins/go_custom_tags/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleysbmartins%2Fgo_custom_tags/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33137831,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T09:28:26.183Z","status":"ssl_error","status_checked_at":"2026-05-17T09:27:52.702Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["customized-tags","fields","golang","reflect","struct","tags"],"created_at":"2024-11-24T13:09:04.139Z","updated_at":"2026-05-17T12:05:13.629Z","avatar_url":"https://github.com/wesleysbmartins.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Golang Custom Tags\nCriação e validação de tags customizadas para structs em golang, usando o pacote nativo reflect.\n\n\n## Entidade\nA entidade users contém a tag customizada **required**, onde iremos validar posteriormente.\n```go\npackage entities\n\ntype User struct {\n\tName     string `required:\"true\"`\n\tAge      int\n\tEmail    string `required:\"true\"`\n\tPassword string `required:\"true\"`\n}\n```\n\n## Validação\nPara a validação utilizaremos o pacote **reflect** do Go, onde é possível identificar os tipos, nomes e valores da struct e seus campos.\n```go\npackage tags\n\nimport (\n\t\"fmt\"\n\t\"reflect\"\n)\n\ntype CustomTags struct{}\n\ntype ICustomTags interface {\n\tValidate(s interface{}) error\n}\n\n// utilizando generics para a struct que validaremos\nfunc (t *CustomTags) Validate(s interface{}) error {\n    // tipo da struct (entities.User)\n\ttypeStruct := reflect.TypeOf(s)\n\n    //valor da struct\n\tvalueStruct := reflect.ValueOf(s)\n\n    // iterando struct para passar por todos os campos\n\tfor i := 0; i \u003c typeStruct.NumField(); i++ {\n        // nome do campo\n\t\tfield := typeStruct.Field(i)\n\n        // buscando tag required\n\t\trequired := field.Tag.Get(\"required\")\n\n        // caso não tenha a flag ou seu valor seja false\n\t\tif required == \"\" || required == \"false\" {\n\t\t\tcontinue\n\t\t}\n\n        // valor do campo\n\t\tvalue := valueStruct.Field(i)\n\n        // validação\n\t\tswitch value.Kind() {\n\t\tcase reflect.String:\n            // se for do tipo string, required true, mas não populado corretamente\n\t\t\tif value.String() == \"\" {\n\t\t\t\treturn fmt.Errorf(\"O campo %s é obrigatório!\", field.Name)\n\t\t\t}\n\n        // se for do tipo int, required true, mas não populado corretamente\n\t\tcase reflect.Int:\n\t\t\tif value.Int() == 0 {\n\t\t\t\treturn fmt.Errorf(\"O campo %s é obrigatório!\", field.Name)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n```\n\n## Main\n```go\npackage main\n\nimport (\n\t\"go_custom_tags/entities\"\n\t\"go_custom_tags/pkg/tags\"\n)\n\nfunc main() {\n\tuser := entities.User{\n\t\tName: \"Wesley Martins\",\n\t\tAge:      25,\n\t\tEmail:    \"email_example@gmail.com\",\n\t\tPassword: \"pass1234\",\n\t}\n\n\ttags := tags.CustomTags{}\n\n\terr := tags.Validate(user)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\nColoque prints ou debugue o método de validação e entenda melhor o que cada operação do reflect retorna:\n```\nSTRUCT:\n    TYPE:  entities.User\n    VALUE:  {Wesley Martins 0 email_example@gmail.com pass1234}\n\nCAMPOS:\n    FIELD:  {Name  string required:\"true\" 0 [0] false}\n    REQUIRED:  true\n    FIELD VALUE:  Wesley Martins\n\n    FIELD:  {Age  int  16 [1] false}\n    REQUIRED:\n\n    FIELD:  {Email  string required:\"true\" 24 [2] false}\n    REQUIRED:  true\n    FIELD VALUE:  email_example@gmail.com\n\n    FIELD:  {Password  string required:\"true\" 40 [3] false}\n    REQUIRED:  true\n    FIELD VALUE:  pass1234\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwesleysbmartins%2Fgo_custom_tags","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwesleysbmartins%2Fgo_custom_tags","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwesleysbmartins%2Fgo_custom_tags/lists"}