{"id":13562954,"url":"https://github.com/creasty/defaults","last_synced_at":"2025-06-14T09:09:12.435Z","repository":{"id":38359734,"uuid":"88877036","full_name":"creasty/defaults","owner":"creasty","description":"Initialize structs with default values","archived":false,"fork":false,"pushed_at":"2024-08-13T06:45:40.000Z","size":55,"stargazers_count":840,"open_issues_count":17,"forks_count":74,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-06-11T07:53:57.137Z","etag":null,"topics":["golang","initialize","map","nested","slice","struct"],"latest_commit_sha":null,"homepage":"","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/creasty.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-20T14:46:26.000Z","updated_at":"2025-06-11T03:03:48.000Z","dependencies_parsed_at":"2024-06-18T12:17:31.811Z","dependency_job_id":"0f92b0f9-0e61-4cd3-8c1f-64166f54e944","html_url":"https://github.com/creasty/defaults","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/creasty/defaults","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creasty%2Fdefaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creasty%2Fdefaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creasty%2Fdefaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creasty%2Fdefaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/creasty","download_url":"https://codeload.github.com/creasty/defaults/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creasty%2Fdefaults/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259790462,"owners_count":22911549,"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":["golang","initialize","map","nested","slice","struct"],"created_at":"2024-08-01T13:01:13.755Z","updated_at":"2025-06-14T09:09:12.407Z","avatar_url":"https://github.com/creasty.png","language":"Go","readme":"defaults\n========\n\n[![CircleCI](https://circleci.com/gh/creasty/defaults/tree/master.svg?style=svg)](https://circleci.com/gh/creasty/defaults/tree/master)\n[![codecov](https://codecov.io/gh/creasty/defaults/branch/master/graph/badge.svg)](https://codecov.io/gh/creasty/defaults)\n[![GitHub release](https://img.shields.io/github/release/creasty/defaults.svg)](https://github.com/creasty/defaults/releases)\n[![License](https://img.shields.io/github/license/creasty/defaults.svg)](./LICENSE)\n\nInitialize structs with default values\n\n- Supports almost all kind of types\n  - Scalar types\n    - `int/8/16/32/64`, `uint/8/16/32/64`, `float32/64`\n    - `uintptr`, `bool`, `string`\n  - Complex types\n    - `map`, `slice`, `struct`\n  - Nested types\n    - `map[K1]map[K2]Struct`, `[]map[K1]Struct[]`\n  - Aliased types\n    - `time.Duration`\n    - e.g., `type Enum string`\n  - Pointer types\n    - e.g., `*SampleStruct`, `*int`\n- Recursively initializes fields in a struct\n- Dynamically sets default values by [`defaults.Setter`](./setter.go) interface\n- Preserves non-initial values from being reset with a default value\n\n\nUsage\n-----\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"math/rand\"\n\n\t\"github.com/creasty/defaults\"\n)\n\ntype Gender string\n\ntype Sample struct {\n\tName    string `default:\"John Smith\"`\n\tAge     int    `default:\"27\"`\n\tGender  Gender `default:\"m\"`\n\tWorking bool   `default:\"true\"`\n\n\tSliceInt    []int    `default:\"[1, 2, 3]\"`\n\tSlicePtr    []*int   `default:\"[1, 2, 3]\"`\n\tSliceString []string `default:\"[\\\"a\\\", \\\"b\\\"]\"`\n\n\tMapNull            map[string]int          `default:\"{}\"`\n\tMap                map[string]int          `default:\"{\\\"key1\\\": 123}\"`\n\tMapOfStruct        map[string]OtherStruct  `default:\"{\\\"Key2\\\": {\\\"Foo\\\":123}}\"`\n\tMapOfPtrStruct     map[string]*OtherStruct `default:\"{\\\"Key3\\\": {\\\"Foo\\\":123}}\"`\n\tMapOfStructWithTag map[string]OtherStruct  `default:\"{\\\"Key4\\\": {\\\"Foo\\\":123}}\"`\n\n\tStruct    OtherStruct  `default:\"{\\\"Foo\\\": 123}\"`\n\tStructPtr *OtherStruct `default:\"{\\\"Foo\\\": 123}\"`\n\n\tNoTag    OtherStruct // Recurses into a nested struct by default\n\tNoOption OtherStruct `default:\"-\"` // no option\n}\n\ntype OtherStruct struct {\n\tHello  string `default:\"world\"` // Tags in a nested struct also work\n\tFoo    int    `default:\"-\"`\n\tRandom int    `default:\"-\"`\n}\n\n// SetDefaults implements defaults.Setter interface\nfunc (s *OtherStruct) SetDefaults() {\n\tif defaults.CanUpdate(s.Random) { // Check if it's a zero value (recommended)\n\t\ts.Random = rand.Int() // Set a dynamic value\n\t}\n}\n\nfunc main() {\n\tobj := \u0026Sample{}\n\tif err := defaults.Set(obj); err != nil {\n\t\tpanic(err)\n\t}\n\n\tout, err := json.MarshalIndent(obj, \"\", \"\t\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(string(out))\n\n\t// Output:\n\t// {\n\t// \t\"Name\": \"John Smith\",\n\t// \t\"Age\": 27,\n\t// \t\"Gender\": \"m\",\n\t// \t\"Working\": true,\n\t// \t\"SliceInt\": [\n\t// \t\t1,\n\t// \t\t2,\n\t// \t\t3\n\t// \t],\n\t// \t\"SlicePtr\": [\n\t// \t\t1,\n\t// \t\t2,\n\t// \t\t3\n\t// \t],\n\t// \t\"SliceString\": [\n\t// \t\t\"a\",\n\t// \t\t\"b\"\n\t// \t],\n\t// \t\"MapNull\": {},\n\t// \t\"Map\": {\n\t// \t\t\"key1\": 123\n\t// \t},\n\t// \t\"MapOfStruct\": {\n\t// \t\t\"Key2\": {\n\t// \t\t\t\"Hello\": \"world\",\n\t// \t\t\t\"Foo\": 123,\n\t// \t\t\t\"Random\": 5577006791947779410\n\t// \t\t}\n\t// \t},\n\t// \t\"MapOfPtrStruct\": {\n\t// \t\t\"Key3\": {\n\t// \t\t\t\"Hello\": \"world\",\n\t// \t\t\t\"Foo\": 123,\n\t// \t\t\t\"Random\": 8674665223082153551\n\t// \t\t}\n\t// \t},\n\t// \t\"MapOfStructWithTag\": {\n\t// \t\t\"Key4\": {\n\t// \t\t\t\"Hello\": \"world\",\n\t// \t\t\t\"Foo\": 123,\n\t// \t\t\t\"Random\": 6129484611666145821\n\t// \t\t}\n\t// \t},\n\t// \t\"Struct\": {\n\t// \t\t\"Hello\": \"world\",\n\t// \t\t\"Foo\": 123,\n\t// \t\t\"Random\": 4037200794235010051\n\t// \t},\n\t// \t\"StructPtr\": {\n\t// \t\t\"Hello\": \"world\",\n\t// \t\t\"Foo\": 123,\n\t// \t\t\"Random\": 3916589616287113937\n\t// \t},\n\t// \t\"NoTag\": {\n\t// \t\t\"Hello\": \"world\",\n\t// \t\t\"Foo\": 0,\n\t// \t\t\"Random\": 6334824724549167320\n\t// \t},\n\t// \t\"NoOption\": {\n\t// \t\t\"Hello\": \"\",\n\t// \t\t\"Foo\": 0,\n\t// \t\t\"Random\": 0\n\t// \t}\n\t// }\n}\n```\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreasty%2Fdefaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcreasty%2Fdefaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreasty%2Fdefaults/lists"}