{"id":13411018,"url":"https://github.com/joho/godotenv","last_synced_at":"2025-05-12T18:30:09.125Z","repository":{"id":37821338,"uuid":"11759482","full_name":"joho/godotenv","owner":"joho","description":"A Go port of Ruby's dotenv library (Loads environment variables from .env files)","archived":false,"fork":false,"pushed_at":"2024-12-16T04:17:10.000Z","size":136,"stargazers_count":9202,"open_issues_count":77,"forks_count":427,"subscribers_count":44,"default_branch":"main","last_synced_at":"2025-05-05T15:56:29.697Z","etag":null,"topics":["dotenv","environment-variables","go","golang"],"latest_commit_sha":null,"homepage":"http://godoc.org/github.com/joho/godotenv","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/joho.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2013-07-30T07:45:19.000Z","updated_at":"2025-05-05T15:38:30.000Z","dependencies_parsed_at":"2024-05-05T03:31:00.245Z","dependency_job_id":"879d1a58-5ce0-4c03-896c-f13461814c41","html_url":"https://github.com/joho/godotenv","commit_stats":{"total_commits":137,"total_committers":41,"mean_commits":3.341463414634146,"dds":0.6131386861313868,"last_synced_commit":"32e64fa834a9f5fcfe7506c2c603d6535da7eb83"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joho%2Fgodotenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joho%2Fgodotenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joho%2Fgodotenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joho%2Fgodotenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joho","download_url":"https://codeload.github.com/joho/godotenv/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253797770,"owners_count":21965961,"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":["dotenv","environment-variables","go","golang"],"created_at":"2024-07-30T20:01:10.878Z","updated_at":"2025-05-12T18:30:09.077Z","avatar_url":"https://github.com/joho.png","language":"Go","readme":"# GoDotEnv ![CI](https://github.com/joho/godotenv/workflows/CI/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/joho/godotenv)](https://goreportcard.com/report/github.com/joho/godotenv)\n\nA Go (golang) port of the Ruby [dotenv](https://github.com/bkeepers/dotenv) project (which loads env vars from a .env file).\n\nFrom the original Library:\n\n\u003e Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.\n\u003e\n\u003e But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv load variables from a .env file into ENV when the environment is bootstrapped.\n\nIt can be used as a library (for loading in env for your own daemons etc.) or as a bin command.\n\nThere is test coverage and CI for both linuxish and Windows environments, but I make no guarantees about the bin version working on Windows.\n\n## Installation\n\nAs a library\n\n```shell\ngo get github.com/joho/godotenv\n```\n\nor if you want to use it as a bin command\n\ngo \u003e= 1.17\n```shell\ngo install github.com/joho/godotenv/cmd/godotenv@latest\n```\n\ngo \u003c 1.17\n```shell\ngo get github.com/joho/godotenv/cmd/godotenv\n```\n\n## Usage\n\nAdd your application configuration to your `.env` file in the root of your project:\n\n```shell\nS3_BUCKET=YOURS3BUCKET\nSECRET_KEY=YOURSECRETKEYGOESHERE\n```\n\nThen in your Go app you can do something like\n\n```go\npackage main\n\nimport (\n    \"log\"\n    \"os\"\n\n    \"github.com/joho/godotenv\"\n)\n\nfunc main() {\n  err := godotenv.Load()\n  if err != nil {\n    log.Fatal(\"Error loading .env file\")\n  }\n\n  s3Bucket := os.Getenv(\"S3_BUCKET\")\n  secretKey := os.Getenv(\"SECRET_KEY\")\n\n  // now do something with s3 or whatever\n}\n```\n\nIf you're even lazier than that, you can just take advantage of the autoload package which will read in `.env` on import\n\n```go\nimport _ \"github.com/joho/godotenv/autoload\"\n```\n\nWhile `.env` in the project root is the default, you don't have to be constrained, both examples below are 100% legit\n\n```go\ngodotenv.Load(\"somerandomfile\")\ngodotenv.Load(\"filenumberone.env\", \"filenumbertwo.env\")\n```\n\nIf you want to be really fancy with your env file you can do comments and exports (below is a valid env file)\n\n```shell\n# I am a comment and that is OK\nSOME_VAR=someval\nFOO=BAR # comments at line end are OK too\nexport BAR=BAZ\n```\n\nOr finally you can do YAML(ish) style\n\n```yaml\nFOO: bar\nBAR: baz\n```\n\nas a final aside, if you don't want godotenv munging your env you can just get a map back instead\n\n```go\nvar myEnv map[string]string\nmyEnv, err := godotenv.Read()\n\ns3Bucket := myEnv[\"S3_BUCKET\"]\n```\n\n... or from an `io.Reader` instead of a local file\n\n```go\nreader := getRemoteFile()\nmyEnv, err := godotenv.Parse(reader)\n```\n\n... or from a `string` if you so desire\n\n```go\ncontent := getRemoteFileContent()\nmyEnv, err := godotenv.Unmarshal(content)\n```\n\n### Precedence \u0026 Conventions\n\nExisting envs take precedence of envs that are loaded later.\n\nThe [convention](https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use)\nfor managing multiple environments (i.e. development, test, production)\nis to create an env named `{YOURAPP}_ENV` and load envs in this order:\n\n```go\nenv := os.Getenv(\"FOO_ENV\")\nif \"\" == env {\n  env = \"development\"\n}\n\ngodotenv.Load(\".env.\" + env + \".local\")\nif \"test\" != env {\n  godotenv.Load(\".env.local\")\n}\ngodotenv.Load(\".env.\" + env)\ngodotenv.Load() // The Original .env\n```\n\nIf you need to, you can also use `godotenv.Overload()` to defy this convention\nand overwrite existing envs instead of only supplanting them. Use with caution.\n\n### Command Mode\n\nAssuming you've installed the command as above and you've got `$GOPATH/bin` in your `$PATH`\n\n```\ngodotenv -f /some/path/to/.env some_command with some args\n```\n\nIf you don't specify `-f` it will fall back on the default of loading `.env` in `PWD`\n\nBy default, it won't override existing environment variables; you can do that with the `-o` flag.\n\n### Writing Env Files\n\nGodotenv can also write a map representing the environment to a correctly-formatted and escaped file\n\n```go\nenv, err := godotenv.Unmarshal(\"KEY=value\")\nerr := godotenv.Write(env, \"./.env\")\n```\n\n... or to a string\n\n```go\nenv, err := godotenv.Unmarshal(\"KEY=value\")\ncontent, err := godotenv.Marshal(env)\n```\n\n## Contributing\n\nContributions are welcome, but with some caveats.\n\nThis library has been declared feature complete (see [#182](https://github.com/joho/godotenv/issues/182) for background) and will not be accepting issues or pull requests adding new functionality or breaking the library API.\n\nContributions would be gladly accepted that:\n\n* bring this library's parsing into closer compatibility with the mainline dotenv implementations, in particular [Ruby's dotenv](https://github.com/bkeepers/dotenv) and [Node.js' dotenv](https://github.com/motdotla/dotenv)\n* keep the library up to date with the go ecosystem (ie CI bumps, documentation changes, changes in the core libraries)\n* bug fixes for use cases that pertain to the library's purpose of easing development of codebases deployed into twelve factor environments\n\n*code changes without tests and references to peer dotenv implementations will not be accepted*\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\n## Releases\n\nReleases should follow [Semver](http://semver.org/) though the first couple of releases are `v1` and `v1.1`.\n\nUse [annotated tags for all releases](https://github.com/joho/godotenv/issues/30). Example `git tag -a v1.2.1`\n\n## Who?\n\nThe original library [dotenv](https://github.com/bkeepers/dotenv) was written by [Brandon Keepers](http://opensoul.org/), and this port was done by [John Barton](https://johnbarton.co/) based off the tests/fixtures in the original library.\n","funding_links":[],"categories":["Popular","Configuration","配置","开源类库","Go","Open source library","语言资源库","配置管理","Utilities","Repositories","\u003cspan id=\"组态-configuration\"\u003e组态 Configuration\u003c/span\u003e","配置管理 `配置解析库`","Uncategorized","\u003ca name=\"Go\"\u003e\u003c/a\u003eGo"],"sub_categories":["Advanced Console UIs","标准CLI","配置","Standard CLI","Construction","go","标准 CLI","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoho%2Fgodotenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoho%2Fgodotenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoho%2Fgodotenv/lists"}