{"id":16095544,"url":"https://github.com/meowgorithm/defaults","last_synced_at":"2025-06-17T15:34:11.419Z","repository":{"id":41298104,"uuid":"418563668","full_name":"meowgorithm/defaults","owner":"meowgorithm","description":"Enforce default values on structs in Go","archived":false,"fork":false,"pushed_at":"2022-08-15T18:22:12.000Z","size":6,"stargazers_count":29,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T13:11:11.722Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/meowgorithm.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":"2021-10-18T15:35:24.000Z","updated_at":"2024-11-20T07:37:22.000Z","dependencies_parsed_at":"2022-09-16T20:51:46.102Z","dependency_job_id":null,"html_url":"https://github.com/meowgorithm/defaults","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/meowgorithm/defaults","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meowgorithm%2Fdefaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meowgorithm%2Fdefaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meowgorithm%2Fdefaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meowgorithm%2Fdefaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/meowgorithm","download_url":"https://codeload.github.com/meowgorithm/defaults/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meowgorithm%2Fdefaults/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260388527,"owners_count":23001540,"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":[],"created_at":"2024-10-09T17:06:19.529Z","updated_at":"2025-06-17T15:34:06.404Z","avatar_url":"https://github.com/meowgorithm.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Defaults\n========\n\nEnforce default values on struct fields.\n\n```go\ntype User struct {\n\tName     string  `default:\"Goku\"`\n\tPower    float64 `default:\"9000.01\"`\n}\n\nvar u User\n\nerr := defaults.Apply(\u0026u)\nif err != nil {\n\tlog.Fatal(\"Uh oh: %v\", err)\n}\n\nfmt.Print(u.Name)  // Goku\nfmt.Print(u.Power) // 9000.01\n```\n\nDefaults are only applied to fields at their zero value.\n\n```go\ntype Config struct {\n\tHost  *string `default:\"0.0.0.0\"`\n\tPort  *int    `default:\"8000\"`\n}\n\nvar cfg Config\njson.Unmarshal([]byte(`{Host: \"charm.sh\"}`), \u0026cfg)\n\nif err := defaults.Apply(\u0026cfg); err != nil {\n\tlog.Fatal(\"Rats: %v\", err)\n}\n\nfmt.Print(cfg.Host) // charm.sh\nfmt.Print(cfg.Port) // 8000\n```\n\nWorks well with JSON, Yaml, and so on.\n\n```go\ntype Config struct {\n    Host  string `json:\"host\" default:\"0.0.0.0\"`\n    Port  int    `json:\"port\" default:\"8000\"`\n    Debug bool   `json:\"debug\" default:\"true\"`\n}\n```\n\n## Supported Types\n\nThe following types are supported:\n\n* `string`\n* `bool`\n* `int`\n* `int8`\n* `int16`\n* `int32` (and `rune`, with some caveats)\n* `int64`\n* `uint`\n* `uint8`\n* `uint16`\n* `uint32`\n* `uint64`\n* `float32`\n* `float64`\n* `[]byte`/`[]uint8`\n\n…as well as pointers to those types.\n\n### Embedded Structs\n\nEmbedded structs are supported. The following will parse as expected:\n\n```go\ntype GroceryList struct {\n\tFruit struct {\n\t\tBananas int `default:\"8\"`\n\t\tPears   int `default:\"12\"`\n\t}\n\tVegetables *struct {\n\t\tArtichokes    int `default:\"4\"`\n\t\tSweetPotatoes int `default:\"16\"`\n\t}\n}\n```\n\nEmbedded structs do not need a `default` tag in order to be parsed. Embedded\nstructs that are `nil` will be initialized with their zero value so they can be\nparsed accoringly.\n\n### Runes and Int32s\n\nIn Go `rune` is just an alias for `int32`. This presents some ambiguity when\nparsing default values. For example, should `\"1\"` be parsed as a literal `1` or\nas a unicode `'1'` (which has the `int32` value of `49`)?\n\nBecause of this ambiguity we recommend avoiding setting defaults on `rune`s.\nThat said, this package defaults to parsing `int32` as integers. Failing that,\nit tries to parse them as a `rune`.\n\n```go\n// This works as expected...\ntype Cool struct {\n\tFave32BitInteger int32 `default:\"12\"`\n\tFaveChar         rune  `default:\"a\"`\n}\n\n// ...but these will not.\ntype UhOh struct {\n\tFaveChar rune `default:\"3\"`  // this is a unicode ETX or ctrl+c\n\tFaveChar rune `default:\"97\"` // this is a unicode `a`\n}\n```\n\n## Licnse\n\n[MIT](https://github.com/meowgorithm/defaults/raw/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmeowgorithm%2Fdefaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmeowgorithm%2Fdefaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmeowgorithm%2Fdefaults/lists"}