{"id":13493020,"url":"https://github.com/fatih/structs","last_synced_at":"2025-10-05T18:31:23.655Z","repository":{"id":37677904,"uuid":"22284914","full_name":"fatih/structs","owner":"fatih","description":"Utilities for Go structs","archived":true,"fork":false,"pushed_at":"2018-10-10T23:18:10.000Z","size":141,"stargazers_count":3911,"open_issues_count":0,"forks_count":704,"subscribers_count":66,"default_branch":"master","last_synced_at":"2025-01-15T05:54:06.326Z","etag":null,"topics":["go","golang","structs"],"latest_commit_sha":null,"homepage":"http://godoc.org/github.com/fatih/structs","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fatih.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-26T11:08:52.000Z","updated_at":"2025-01-14T09:12:14.000Z","dependencies_parsed_at":"2022-07-13T09:20:32.748Z","dependency_job_id":null,"html_url":"https://github.com/fatih/structs","commit_stats":null,"previous_names":["fatih/structure"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatih%2Fstructs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatih%2Fstructs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatih%2Fstructs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fatih%2Fstructs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fatih","download_url":"https://codeload.github.com/fatih/structs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235432243,"owners_count":18989483,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["go","golang","structs"],"created_at":"2024-07-31T19:01:11.397Z","updated_at":"2025-10-05T18:31:23.336Z","avatar_url":"https://github.com/fatih.png","language":"Go","readme":"# Archived project. No maintenance. \n\nThis project is not maintained anymore and is archived. Feel free to fork and\nmake your own changes if needed. For more detail read my blog post: [Taking an indefinite sabbatical from my projects](https://arslan.io/2018/10/09/taking-an-indefinite-sabbatical-from-my-projects/)\n\nThanks to everyone for their valuable feedback and contributions.\n\n# Structs [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/fatih/structs) [![Build Status](http://img.shields.io/travis/fatih/structs.svg?style=flat-square)](https://travis-ci.org/fatih/structs) [![Coverage Status](http://img.shields.io/coveralls/fatih/structs.svg?style=flat-square)](https://coveralls.io/r/fatih/structs)\n\nStructs contains various utilities to work with Go (Golang) structs. It was\ninitially used by me to convert a struct into a `map[string]interface{}`. With\ntime I've added other utilities for structs.  It's basically a high level\npackage based on primitives from the reflect package. Feel free to add new\nfunctions or improve the existing code.\n\n## Install\n\n```bash\ngo get github.com/fatih/structs\n```\n\n## Usage and Examples\n\nJust like the standard lib `strings`, `bytes` and co packages, `structs` has\nmany global functions to manipulate or organize your struct data. Lets define\nand declare a struct:\n\n```go\ntype Server struct {\n\tName        string `json:\"name,omitempty\"`\n\tID          int\n\tEnabled     bool\n\tusers       []string // not exported\n\thttp.Server          // embedded\n}\n\nserver := \u0026Server{\n\tName:    \"gopher\",\n\tID:      123456,\n\tEnabled: true,\n}\n```\n\n```go\n// Convert a struct to a map[string]interface{}\n// =\u003e {\"Name\":\"gopher\", \"ID\":123456, \"Enabled\":true}\nm := structs.Map(server)\n\n// Convert the values of a struct to a []interface{}\n// =\u003e [\"gopher\", 123456, true]\nv := structs.Values(server)\n\n// Convert the names of a struct to a []string\n// (see \"Names methods\" for more info about fields)\nn := structs.Names(server)\n\n// Convert the values of a struct to a []*Field\n// (see \"Field methods\" for more info about fields)\nf := structs.Fields(server)\n\n// Return the struct name =\u003e \"Server\"\nn := structs.Name(server)\n\n// Check if any field of a struct is initialized or not.\nh := structs.HasZero(server)\n\n// Check if all fields of a struct is initialized or not.\nz := structs.IsZero(server)\n\n// Check if server is a struct or a pointer to struct\ni := structs.IsStruct(server)\n```\n\n### Struct methods\n\nThe structs functions can be also used as independent methods by creating a new\n`*structs.Struct`. This is handy if you want to have more control over the\nstructs (such as retrieving a single Field).\n\n```go\n// Create a new struct type:\ns := structs.New(server)\n\nm := s.Map()              // Get a map[string]interface{}\nv := s.Values()           // Get a []interface{}\nf := s.Fields()           // Get a []*Field\nn := s.Names()            // Get a []string\nf := s.Field(name)        // Get a *Field based on the given field name\nf, ok := s.FieldOk(name)  // Get a *Field based on the given field name\nn := s.Name()             // Get the struct name\nh := s.HasZero()          // Check if any field is uninitialized\nz := s.IsZero()           // Check if all fields are uninitialized\n```\n\n### Field methods\n\nWe can easily examine a single Field for more detail. Below you can see how we\nget and interact with various field methods:\n\n\n```go\ns := structs.New(server)\n\n// Get the Field struct for the \"Name\" field\nname := s.Field(\"Name\")\n\n// Get the underlying value,  value =\u003e \"gopher\"\nvalue := name.Value().(string)\n\n// Set the field's value\nname.Set(\"another gopher\")\n\n// Get the field's kind, kind =\u003e  \"string\"\nname.Kind()\n\n// Check if the field is exported or not\nif name.IsExported() {\n\tfmt.Println(\"Name field is exported\")\n}\n\n// Check if the value is a zero value, such as \"\" for string, 0 for int\nif !name.IsZero() {\n\tfmt.Println(\"Name is initialized\")\n}\n\n// Check if the field is an anonymous (embedded) field\nif !name.IsEmbedded() {\n\tfmt.Println(\"Name is not an embedded field\")\n}\n\n// Get the Field's tag value for tag name \"json\", tag value =\u003e \"name,omitempty\"\ntagValue := name.Tag(\"json\")\n```\n\nNested structs are supported too:\n\n```go\naddrField := s.Field(\"Server\").Field(\"Addr\")\n\n// Get the value for addr\na := addrField.Value().(string)\n\n// Or get all fields\nhttpServer := s.Field(\"Server\").Fields()\n```\n\nWe can also get a slice of Fields from the Struct type to iterate over all\nfields. This is handy if you wish to examine all fields:\n\n```go\ns := structs.New(server)\n\nfor _, f := range s.Fields() {\n\tfmt.Printf(\"field name: %+v\\n\", f.Name())\n\n\tif f.IsExported() {\n\t\tfmt.Printf(\"value   : %+v\\n\", f.Value())\n\t\tfmt.Printf(\"is zero : %+v\\n\", f.IsZero())\n\t}\n}\n```\n\n## Credits\n\n * [Fatih Arslan](https://github.com/fatih)\n * [Cihangir Savas](https://github.com/cihangir)\n\n## License\n\nThe MIT License (MIT) - see LICENSE.md for more details\n","funding_links":[],"categories":["Go","Serialization","序列化"],"sub_categories":["Advanced Console UIs","交流"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffatih%2Fstructs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffatih%2Fstructs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffatih%2Fstructs/lists"}