{"id":36728913,"url":"https://github.com/obalunenko/getenv","last_synced_at":"2026-01-12T12:04:27.178Z","repository":{"id":37247985,"uuid":"433887792","full_name":"obalunenko/getenv","owner":"obalunenko","description":"Package getenv provides type-safe generic environment variables parser.","archived":false,"fork":false,"pushed_at":"2025-11-11T21:26:34.000Z","size":17268,"stargazers_count":6,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-11T23:23:50.568Z","etag":null,"topics":["environment-variable-processor","environment-variables","generics-in-golang","go","golang","variable-parser"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/obalunenko/getenv","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/obalunenko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-12-01T15:46:06.000Z","updated_at":"2025-11-11T21:26:37.000Z","dependencies_parsed_at":"2023-12-24T12:35:35.615Z","dependency_job_id":"7ef18f5d-6312-481f-9483-20d828cfeb3a","html_url":"https://github.com/obalunenko/getenv","commit_stats":{"total_commits":126,"total_committers":2,"mean_commits":63.0,"dds":0.4920634920634921,"last_synced_commit":"6367aec0fcc52ecb86a082170c3fa839a98601d3"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"purl":"pkg:github/obalunenko/getenv","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obalunenko%2Fgetenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obalunenko%2Fgetenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obalunenko%2Fgetenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obalunenko%2Fgetenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/obalunenko","download_url":"https://codeload.github.com/obalunenko/getenv/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obalunenko%2Fgetenv/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28338975,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T10:58:46.209Z","status":"ssl_error","status_checked_at":"2026-01-12T10:58:42.742Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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":["environment-variable-processor","environment-variables","generics-in-golang","go","golang","variable-parser"],"created_at":"2026-01-12T12:04:27.118Z","updated_at":"2026-01-12T12:04:27.172Z","avatar_url":"https://github.com/obalunenko.png","language":"Go","readme":"![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/obalunenko/getenv)\n[![Go Reference](https://pkg.go.dev/badge/github.com/obalunenko/getenv.svg)](https://pkg.go.dev/github.com/obalunenko/getenv)\n[![Go Report Card](https://goreportcard.com/badge/github.com/obalunenko/getenv)](https://goreportcard.com/report/github.com/obalunenko/getenv)\n[![codecov](https://codecov.io/gh/obalunenko/getenv/branch/master/graph/badge.svg)](https://codecov.io/gh/obalunenko/getenv)\n![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-88.38%25-brightgreen?longCache=true\u0026style=flat)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=obalunenko_getenv\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=obalunenko_getenv)\n\n# getenv\n\nPackage getenv provides a simple way to get environment variables.\nIt's type-safe and supports built-in types and slices of them.\n\nTypes supported:\n- string\n- []string\n- int\n- []int\n- int8\n- []int8\n- int16\n- []int16\n- int32\n- []int32\n- int64\n- []int64\n- uint8\n- []uint8\n- uint16\n- []uint16\n- uint64\n- []uint64\n- uint\n- []uint\n- uintptr\n- []uintptr\n- uint32\n- []uint32\n- float32\n- []float32\n- float64\n- []float64\n- time.Time\n- []time.Time\n- time.Duration\n- []time.Duration\n- bool\n- []bool\n- url.URL\n- []url.URL\n- net.IP\n- []net.IP\n- complex64\n- []complex64\n- complex128\n- []complex128\n\n## Examples\n\n### Env\n\nEnv retrieves the value of the environment variable named by the key.\nIf the variable is present in the environment the value will be parsed and returned.\nOtherwise, an error will be returned.\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net\"\n\t\"net/url\"\n\t\"os\"\n\t\"time\"\n\n\t\"github.com/obalunenko/getenv\"\n\t\"github.com/obalunenko/getenv/option\"\n)\n\nfunc main() {\n\tconst (\n\t\tkey = \"GH_GETENV_TEST\"\n\t)\n\n\tvar (\n\t\tval any\n\t\terr error\n\t)\n\n\tdefer func() {\n\t\tif err = os.Unsetenv(key); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}()\n\n\t// string\n\tif err = os.Setenv(key, \"golly\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[string](key)\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// int\n\tif err = os.Setenv(key, \"123\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[int](key)\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// int conversion error\n\tif err = os.Setenv(key, \"123s4\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[int](key)\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// time.Time\n\tif err = os.Setenv(key, \"2022-01-20\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[time.Time](key, option.WithTimeLayout(\"2006-01-02\"))\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// []float64\n\tif err = os.Setenv(key, \"26.89,0.67\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[[]float64](key, option.WithSeparator(\",\"))\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// time.Duration\n\tif err = os.Setenv(key, \"2h35m\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[time.Duration](key)\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// url.URL\n\tif err = os.Setenv(key, \"https://test:abcd123@golangbyexample.com:8000/tutorials/intro?type=advance\u0026compact=false#history\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[url.URL](key)\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// net.IP\n\tif err = os.Setenv(key, \"2001:cb8::17\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[net.IP](key)\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// []string\n\tif err = os.Setenv(key, \"a,b,c,d\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[[]string](key, option.WithSeparator(\",\"))\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// complex128\n\tif err = os.Setenv(key, \"1+2i\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[complex128](key)\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n\t// []complex64\n\tif err = os.Setenv(key, \"1+2i,3+4i\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err = getenv.Env[[]complex64](key, option.WithSeparator(\",\"))\n\tfmt.Printf(\"[%T]: %v; err: %v\\n\", val, val, err)\n\n}\n\n```\n\nOutput:\n\n```\n[string]: golly; err: \u003cnil\u003e\n[int]: 123; err: \u003cnil\u003e\n[int]: 0; err: could not parse variable[GH_GETENV_TEST] value[123s4] to type[int]: invalid value\n[time.Time]: 2022-01-20 00:00:00 +0000 UTC; err: \u003cnil\u003e\n[[]float64]: [26.89 0.67]; err: \u003cnil\u003e\n[time.Duration]: 2h35m0s; err: \u003cnil\u003e\n[url.URL]: {https  test:abcd123 golangbyexample.com:8000 /tutorials/intro  false false type=advance\u0026compact=false history }; err: \u003cnil\u003e\n[net.IP]: 2001:cb8::17; err: \u003cnil\u003e\n[[]string]: [a b c d]; err: \u003cnil\u003e\n[complex128]: (1+2i); err: \u003cnil\u003e\n[[]complex64]: [(1+2i) (3+4i)]; err: \u003cnil\u003e\n```\n\n### EnvOrDefault\n\nEnvOrDefault retrieves the value of the environment variable named by the key.\nIf the variable is present in the environment the value will be parsed and returned.\nOtherwise, the default value will be returned.\nThe value returned will be of the same type as the default value.\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net\"\n\t\"net/url\"\n\t\"os\"\n\t\"time\"\n\n\t\"github.com/obalunenko/getenv\"\n\t\"github.com/obalunenko/getenv/option\"\n)\n\nfunc main() {\n\tconst (\n\t\tkey = \"GH_GETENV_TEST\"\n\t)\n\n\tdefer func() {\n\t\tif err := os.Unsetenv(key); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}()\n\n\tvar val any\n\n\t// string\n\tif err := os.Setenv(key, \"golly\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, \"golly\")\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// int\n\tif err := os.Setenv(key, \"123\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, -99)\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// time.Time\n\tif err := os.Setenv(key, \"2022-01-20\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key,\n\t\ttime.Date(1992, 12, 1, 0, 0, 0, 0, time.UTC),\n\t\toption.WithTimeLayout(\"2006-01-02\"),\n\t)\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// []float64\n\tif err := os.Setenv(key, \"26.89,0.67\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, []float64{-99},\n\t\toption.WithSeparator(\",\"),\n\t)\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// time.Duration\n\tif err := os.Setenv(key, \"2h35m\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, time.Second)\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// url.URL\n\tif err := os.Setenv(key, \"https://test:abcd123@golangbyexample.com:8000/tutorials/intro?type=advance\u0026compact=false#history\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, url.URL{})\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// net.IP\n\tif err := os.Setenv(key, \"2001:cb8::17\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, net.IP{})\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// []string\n\tif err := os.Setenv(key, \"a,b,c,d\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, []string{}, option.WithSeparator(\",\"))\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// complex128\n\tif err := os.Setenv(key, \"1+2i\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, complex128(0))\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n\t// []complex64\n\tif err := os.Setenv(key, \"1+2i,3+4i\"); err != nil {\n\t\tpanic(err)\n\t}\n\n\tval = getenv.EnvOrDefault(key, []complex64{}, option.WithSeparator(\",\"))\n\tfmt.Printf(\"[%T]: %v\\n\", val, val)\n\n}\n\n```\n\nOutput:\n\n```\n[string]: golly\n[int]: 123\n[time.Time]: 2022-01-20 00:00:00 +0000 UTC\n[[]float64]: [26.89 0.67]\n[time.Duration]: 2h35m0s\n[url.URL]: {https  test:abcd123 golangbyexample.com:8000 /tutorials/intro  false false type=advance\u0026compact=false history }\n[net.IP]: 2001:cb8::17\n[[]string]: [a b c d]\n[complex128]: (1+2i)\n[[]complex64]: [(1+2i) (3+4i)]\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fobalunenko%2Fgetenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fobalunenko%2Fgetenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fobalunenko%2Fgetenv/lists"}