{"id":17950965,"url":"https://github.com/qiangxue/go-env","last_synced_at":"2025-03-25T00:31:24.284Z","repository":{"id":56604871,"uuid":"226968495","full_name":"qiangxue/go-env","owner":"qiangxue","description":"A Go (golang) library for populating a struct with the values from environment variables","archived":false,"fork":false,"pushed_at":"2020-10-29T08:11:43.000Z","size":18,"stargazers_count":26,"open_issues_count":1,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-19T05:33:01.211Z","etag":null,"topics":["config","configuration","env","environment","go","golang","library","struct","unmarshal"],"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/qiangxue.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":"2019-12-09T21:11:49.000Z","updated_at":"2024-07-16T20:09:27.000Z","dependencies_parsed_at":"2022-08-15T21:50:17.696Z","dependency_job_id":null,"html_url":"https://github.com/qiangxue/go-env","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qiangxue%2Fgo-env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qiangxue%2Fgo-env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qiangxue%2Fgo-env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qiangxue%2Fgo-env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qiangxue","download_url":"https://codeload.github.com/qiangxue/go-env/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245377920,"owners_count":20605374,"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":["config","configuration","env","environment","go","golang","library","struct","unmarshal"],"created_at":"2024-10-29T09:41:42.361Z","updated_at":"2025-03-25T00:31:24.031Z","avatar_url":"https://github.com/qiangxue.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-env\n\n[![GoDoc](https://godoc.org/github.com/qiangxue/go-env?status.png)](http://godoc.org/github.com/qiangxue/go-env)\n[![Build Status](https://travis-ci.org/qiangxue/go-env.svg?branch=master)](https://travis-ci.org/qiangxue/go-env)\n[![Coverage Status](https://coveralls.io/repos/github/qiangxue/go-env/badge.svg?branch=master)](https://coveralls.io/github/qiangxue/go-env?branch=master)\n[![Go Report](https://goreportcard.com/badge/github.com/qiangxue/go-env)](https://goreportcard.com/report/github.com/qiangxue/go-env)\n\n## Description\n\ngo-env is a Go library that can populate a struct with environment variable values. A common use of go-env is\nto load a configuration struct with values set in the environment variables.\n\n## Requirements\n\nGo 1.13 or above.\n\n\n## Getting Started\n\n### Installation\n\nRun the following command to install the package:\n\n```\ngo get github.com/qiangxue/go-env\n```\n\n### Loading From Environment Variables\n\nThe easiest way of using go-env is to call `env.Load()`, like the following:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/qiangxue/go-env\"\n\t\"os\"\n)\n\ntype Config struct {\n\tHost string\n\tPort int\n}\n\nfunc main() {\n\t_ = os.Setenv(\"APP_HOST\", \"127.0.0.1\")\n\t_ = os.Setenv(\"APP_PORT\", \"8080\")\n\n\tvar cfg Config\n\tif err := env.Load(\u0026cfg); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(cfg.Host)\n\tfmt.Println(cfg.Port)\n\t// Output:\n\t// 127.0.0.1\n\t// 8080\n}\n```\n\n### Environment Variable Names\n\nWhen go-env populates a struct from environment variables, it uses the following rules to match\na struct field with an environment variable:\n- Only public struct fields will be populated\n- If the field has an `env` tag, use the tag value as the name, unless the tag value is `-` in which case it means\n  the field should NOT be populated.\n- If the field has no `env` tag, turn the field name into UPPER_SNAKE_CASE format and use that as the name. For example,\n  a field name `HostName` will be turned into `HOST_NAME`, and `MyURL` becomes `MY_URL`.\n- Names are prefixed with the specified prefix when they are used to look up in the environment variables.\n\nBy default, prefix `APP_` will be used. You can customize the prefix by using `env.New()` to create\na customized loader. For example,\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/qiangxue/go-env\"\n\t\"log\"\n\t\"os\"\n)\n\ntype Config struct {\n\tHost     string `env:\"ES_HOST\"`\n\tPort     int    `env:\"ES_PORT\"`\n\tPassword string `env:\"ES_PASSWORD,secret\"`\n}\n\nfunc main() {\n\t_ = os.Setenv(\"API_ES_HOST\", \"127.0.0.1\")\n\t_ = os.Setenv(\"API_ES_PORT\", \"8080\")\n\t_ = os.Setenv(\"API_ES_PASSWORD\", \"test\")\n\n\tvar cfg Config\n\tloader := env.New(\"API_\", log.Printf)\n\tif err := loader.Load(\u0026cfg); err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(cfg.Host)\n\tfmt.Println(cfg.Port)\n\tfmt.Println(cfg.Password)\n\t// Output:\n\t// 127.0.0.1\n\t// 8080\n\t// test\n}\n```\n\nIn the above code, the `Password` field is tagged as `secret`. The log function respects this flag by masking\nthe field value when logging it in order not to reveal sensitive information.\n\nBy setting the prefix to an empty string, you can disable the name prefix completely.\n\n\n### Data Parsing Rules\n\nBecause the values of environment variables are strings, if the corresponding struct fields are of different types,\ngo-env will convert the string values into appropriate types before assigning them to the struct fields.\n\n- If a struct contains embedded structs, the fields of the embedded structs will be populated like they are directly\nunder the containing struct.\n\n- If a struct field type implements `env.Setter`, `env.TextMarshaler`, or `env.BinaryMarshaler` interface,\nthe corresponding interface method will be used to load a string value into the field.\n\n- If a struct field is of a primary type, such as `int`, `string`, `bool`, etc., a string value will be parsed\naccordingly and assigned to the field. For example, the string value `TRUE` can be parsed correctly into a\nboolean `true` value, while `TrUE` will cause a parsing error.\n\n- If a struct field is of a complex type, such as map, slice, struct, the string value will be treated as a JSON\nstring, and `json.Unmarshal()` will be called to populate the struct field from the JSON string.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqiangxue%2Fgo-env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqiangxue%2Fgo-env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqiangxue%2Fgo-env/lists"}