{"id":13811941,"url":"https://github.com/Netflix/go-env","last_synced_at":"2025-05-14T20:31:07.811Z","repository":{"id":33800850,"uuid":"134337666","full_name":"Netflix/go-env","owner":"Netflix","description":"a golang library to manage environment variables","archived":false,"fork":false,"pushed_at":"2022-09-28T15:18:35.000Z","size":58,"stargazers_count":546,"open_issues_count":7,"forks_count":56,"subscribers_count":292,"default_branch":"master","last_synced_at":"2024-04-14T22:21:33.443Z","etag":null,"topics":["environment-variables","golang"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Netflix.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":"2018-05-22T00:01:45.000Z","updated_at":"2024-03-31T17:49:47.000Z","dependencies_parsed_at":"2022-07-31T23:38:11.622Z","dependency_job_id":null,"html_url":"https://github.com/Netflix/go-env","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Netflix%2Fgo-env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Netflix%2Fgo-env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Netflix%2Fgo-env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Netflix%2Fgo-env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Netflix","download_url":"https://codeload.github.com/Netflix/go-env/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254222114,"owners_count":22034822,"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":["environment-variables","golang"],"created_at":"2024-08-04T04:00:42.023Z","updated_at":"2025-05-14T20:31:07.543Z","avatar_url":"https://github.com/Netflix.png","language":"Go","readme":"# go-env\n\n![Build Status](https://github.com/Netflix/go-env/actions/workflows/build.yml/badge.svg)\n[![Go Reference](https://pkg.go.dev/badge/github.com/Netflix/go-env.svg)](https://pkg.go.dev/github.com/Netflix/go-env)\n[![NetflixOSS Lifecycle](https://img.shields.io/osslifecycle/Netflix/go-expect.svg)]()\n\n\nPackage env provides an `env` struct field tag to marshal and unmarshal environment variables.\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/Netflix/go-env\"\n)\n\ntype Environment struct {\n\tHome string `env:\"HOME\"`\n\n\tJenkins struct {\n\t\tBuildId     *string `env:\"BUILD_ID\"`\n\t\tBuildNumber int     `env:\"BUILD_NUMBER\"`\n\t\tCi          bool    `env:\"CI\"`\n\t}\n\n\tNode struct {\n\t\tConfigCache *string `env:\"npm_config_cache,NPM_CONFIG_CACHE\"`\n\t}\n\n\tExtras env.EnvSet\n\n\tDuration      time.Duration `env:\"TYPE_DURATION\"`\n\tDefaultValue  string        `env:\"MISSING_VAR,default=default_value\"`\n\tRequiredValue string        `env:\"IM_REQUIRED,required=true\"`\n\tArrayValue    []string      `env:\"ARRAY_VALUE,default=value1|value2|value3\"`\n}\n\nfunc main() {\n\tvar environment Environment\n\tes, err := env.UnmarshalFromEnviron(\u0026environment)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t// Remaining environment variables.\n\tenvironment.Extras = es\n\n\t// ...\n\n\tes, err = env.Marshal(\u0026environment)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\thome := \"/tmp/edgarl\"\n\tcs := env.ChangeSet{\n\t\t\"HOME\":         \u0026home,\n\t\t\"BUILD_ID\":     nil,\n\t\t\"BUILD_NUMBER\": nil,\n\t}\n\tes.Apply(cs)\n\n\tenvironment = Environment{}\n\tif err = env.Unmarshal(es, \u0026environment); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tenvironment.Extras = es\n}\n```\n\nThis will initially throw an error if `IM_REQUIRED` is not set in the environment as part of the env struct validation.\n\nThis error can be resolved by setting the `IM_REQUIRED` environment variable manually in the environment or by setting it in the \ncode prior to calling `UnmarshalFromEnviron` with:\n```go\nos.Setenv(\"IM_REQUIRED\", \"some_value\")\n```\n\n## Custom Marshaler/Unmarshaler\n\nThere is limited support for dictating how a field should be marshaled or unmarshaled. The following example\nshows how you could marshal/unmarshal from JSON\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"log\"\n\t\n\t\"github.com/Netflix/go-env\"\n)\n\ntype SomeData struct {\n    SomeField int `json:\"someField\"`\n}\n\nfunc (s *SomeData) UnmarshalEnvironmentValue(data string) error {\n    var tmp SomeData\n\tif  err := json.Unmarshal([]byte(data), \u0026tmp); err != nil {\n\t\treturn err\n\t}\n\t*s = tmp \n\treturn nil\n}\n\nfunc (s SomeData) MarshalEnvironmentValue() (string, error) {\n\tbytes, err := json.Marshal(s)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn string(bytes), nil\n}\n\ntype Config struct {\n    SomeData *SomeData `env:\"SOME_DATA\"`\n}\n\nfunc main() {\n\tvar cfg Config\n\tif _, err := env.UnmarshalFromEnviron(\u0026cfg); err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n    if cfg.SomeData != nil \u0026\u0026 cfg.SomeData.SomeField == 42 {\n        fmt.Println(\"Got 42!\")\n    } else {\n        fmt.Printf(\"Got nil or some other value: %v\\n\", cfg.SomeData)\n    }\n\n    es, err := env.Marshal(\u0026cfg)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n    fmt.Printf(\"Got the following: %+v\\n\", es)\n}\n```\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNetflix%2Fgo-env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNetflix%2Fgo-env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNetflix%2Fgo-env/lists"}