{"id":21712138,"url":"https://github.com/ofw/goenvconfig","last_synced_at":"2025-03-20T18:23:06.081Z","repository":{"id":116745082,"uuid":"272488263","full_name":"ofw/goenvconfig","owner":"ofw","description":"Unmarshal env into struct to use as config in Golang; pretty print results too.","archived":false,"fork":false,"pushed_at":"2020-06-15T18:56:43.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-25T17:10:21.570Z","etag":null,"topics":["configuration","environment-variables","golang"],"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/ofw.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":"2020-06-15T16:22:09.000Z","updated_at":"2024-05-13T17:13:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"537148ad-9481-48b9-88cf-0ab3b0040d69","html_url":"https://github.com/ofw/goenvconfig","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofw%2Fgoenvconfig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofw%2Fgoenvconfig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofw%2Fgoenvconfig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofw%2Fgoenvconfig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ofw","download_url":"https://codeload.github.com/ofw/goenvconfig/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244667342,"owners_count":20490423,"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":["configuration","environment-variables","golang"],"created_at":"2024-11-25T23:34:31.896Z","updated_at":"2025-03-20T18:23:06.048Z","avatar_url":"https://github.com/ofw.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# envconfig\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/ofw/goenvconfig)](https://goreportcard.com/report/github.com/ofw/goenvconfig)\n![Go](https://github.com/ofw/goenvconfig/workflows/Go/badge.svg)\n[![GoDoc](https://godoc.org/github.com/ofw/goenvconfig?status.svg)](https://godoc.org/github.com/ofw/goenvconfig/envconfig)\n[![go.dev](https://img.shields.io/badge/go.dev-pkg-007d9c.svg?style=flat)](https://pkg.go.dev/github.com/ofw/goenvconfig/envconfig)\n\n```Go\nimport \"github.com/ofw/goenvconfig/envconfig\"\n```\n\n## Goals\n\nThis library was inspired by [envconfig](https://github.com/kelseyhightower/envconfig) with some goals in mind:\n1. Simplicity: \n    - only flat structure of config. so mapping env variables to fields is obvious.\n    - no guessing of env variable names. just case sensitive match.\n    - all env variables are required to be set. support default values\n2. Observability: \n    - supports pretty printing results of parsing env variables to struct\n3. User-friendliness:\n    - supports ability to pretty print results of unmarshaling env variables to config\n\n## Usage\n\nSet some environment variables:\n\n```Bash\nexport MYAPP_DEBUG=false\nexport MYAPP_PORT=8080\nexport MYAPP_USER=Kelsey\nexport MYAPP_RATE=\"0.5\"\nexport MYAPP_TIMEOUT=\"3m\"\nexport MYAPP_USERS=\"rob,ken,robert\"\nexport MYAPP_COLOR_CODES=\"red:1,green:2,blue:3\"\n```\n\nWrite some code:\n\n```Go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/ofw/goenvconfig/envconfig\"\n)\n\nfunc main() {\n\tvar s struct {\n        Debug      bool           `env:\"MYAPP_DEBUG\"`\n        Port       int            `env:\"MYAPP_PORT\"`\n        User       string         `env:\"MYAPP_USER\"`\n        Users      []string       `env:\"MYAPP_USERS\"`\n        Rate       float32        `env:\"MYAPP_RATE\"`\n        Timeout    time.Duration  `env:\"MYAPP_TIMEOUT\"`\n        ColorCodes map[string]int `env:\"MYAPP_COLOR_CODES\"`\n    }\n\n\tresults, err := envconfig.Unmarshal(\u0026s)\n\tresults.PrettyPrint() // it is nil safe\n\t// Env Variable         Type              OK\n\t// ----                 ----              ----\n\t// MYAPP_COLOR_CODES    map[string]int    v\n\t// MYAPP_DEBUG          bool              v\n\t// MYAPP_PORT           int               v\n\t// MYAPP_RATE           float32           v\n\t// MYAPP_TIMEOUT        time.Duration     v\n\t// MYAPP_USER           string            v\n\t// MYAPP_USERS          []string          v\n\n    fmt.Printf(\"%+v\", s)\n    // Result:\n    // {\n    //  Debug:false \n    //  Port:8080 User:Kelsey \n    //  Users:[rob ken robert] \n    //  Rate:0.5 \n    //  Timeout:3m0s \n    //  ColorCodes:map[blue:3 green:2 red:1]\n    // }\n}\n```\n\n## Struct Tag Support\n\nEnv variable name must be specified using `env` tag.\nIf struct contains field without `env` tag that will result in error.\n\nEnvironment variables are required by default. \nIf this is not desired one can use a `default` tag to specify value \nin case environment variable is not set.\n\nFor example, consider the following struct:\n\n```Go\ntype Specification struct {\n    Default         string `env:\"MYAPP_DEFAULT\" default:\"foobar\"`\n    Foo             string `env:\"MYAPP_FOO\"`\n}\n```\n\nIf envconfig can't find an environment variable `MYAPP_DEFAULT`\nit will populate it with \"foobar\" as a default value.\n\nIf envconfig can't find an environment variable `MYAPP_FOO` it will return an error.\n\n## Supported Struct Field Types\n\nenvconfig supports these struct field types:\n\n  * string\n  * int8, int16, int32, int64\n  * bool\n  * float32, float64\n  * slices of any supported type\n  * maps (keys and values of any supported type)\n  * [encoding.TextUnmarshaler](https://golang.org/pkg/encoding/#TextUnmarshaler)\n  * [encoding.BinaryUnmarshaler](https://golang.org/pkg/encoding/#BinaryUnmarshaler)\n  * [time.Duration](https://golang.org/pkg/time/#Duration)\n\nEmbedded structs using these fields are also supported.\n\n## Custom Decoders\n\nAny field whose type (or pointer-to-type) implements `envconfig.Decoder` can\ncontrol its own deserialization:\n\n```Bash\nexport DNS_SERVER=8.8.8.8\n```\n\n```Go\ntype IPDecoder net.IP\n\nfunc (ipd *IPDecoder) Decode(value string) error {\n    *ipd = IPDecoder(net.ParseIP(value))\n    return nil\n}\n\ntype DNSConfig struct {\n    Address IPDecoder `env:\"DNS_SERVER\"`\n}\n```\n\nAlso, envconfig will use a `Set(string) error` method like from the\n[flag.Value](https://godoc.org/flag#Value) interface if implemented.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fofw%2Fgoenvconfig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fofw%2Fgoenvconfig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fofw%2Fgoenvconfig/lists"}