{"id":13564785,"url":"https://github.com/subosito/gotenv","last_synced_at":"2025-05-13T23:09:24.105Z","repository":{"id":565907,"uuid":"12406004","full_name":"subosito/gotenv","owner":"subosito","description":"Load environment variables from `.env` or `io.Reader` in Go.","archived":false,"fork":false,"pushed_at":"2025-05-05T18:36:53.000Z","size":123,"stargazers_count":304,"open_issues_count":2,"forks_count":35,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-11T03:27:17.457Z","etag":null,"topics":["environment-variables","golang","golang-library"],"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/subosito.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2013-08-27T12:56:47.000Z","updated_at":"2025-05-10T21:57:16.000Z","dependencies_parsed_at":"2023-07-05T15:00:35.373Z","dependency_job_id":"c7de0411-a188-4d5a-ba3d-e390480df34c","html_url":"https://github.com/subosito/gotenv","commit_stats":{"total_commits":87,"total_committers":14,"mean_commits":6.214285714285714,"dds":0.5517241379310345,"last_synced_commit":"0430c38a67f63f852fae985c6b762ca1ec808acd"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/subosito%2Fgotenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/subosito%2Fgotenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/subosito%2Fgotenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/subosito%2Fgotenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/subosito","download_url":"https://codeload.github.com/subosito/gotenv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253710184,"owners_count":21951298,"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","golang-library"],"created_at":"2024-08-01T13:01:35.848Z","updated_at":"2025-05-13T23:09:24.074Z","avatar_url":"https://github.com/subosito.png","language":"Go","readme":"# gotenv\n\n[![Build Status](https://github.com/subosito/gotenv/workflows/Go%20workflow/badge.svg)](https://github.com/subosito/gotenv/actions)\n[![Coverage Status](https://badgen.net/codecov/c/github/subosito/gotenv)](https://codecov.io/gh/subosito/gotenv)\n[![Go Report Card](https://goreportcard.com/badge/github.com/subosito/gotenv)](https://goreportcard.com/report/github.com/subosito/gotenv)\n[![GoDoc](https://godoc.org/github.com/subosito/gotenv?status.svg)](https://godoc.org/github.com/subosito/gotenv)\n\nLoad environment variables from `.env` or `io.Reader` in Go.\n\n## Usage\n\nPut the gotenv package on your `import` statement:\n\n```go\nimport \"github.com/subosito/gotenv\"\n```\n\nTo modify your app environment variables, `gotenv` expose 2 main functions:\n\n- `gotenv.Load`\n- `gotenv.Apply`\n\nBy default, `gotenv.Load` will look for a file called `.env` in the current working directory.\n\nBehind the scene, it will then load `.env` file and export the valid variables to the environment variables. Make sure you call the method as soon as possible to ensure it loads all variables, say, put it on `init()` function.\n\nOnce loaded you can use `os.Getenv()` to get the value of the variable.\n\nLet's say you have `.env` file:\n\n```sh\nAPP_ID=1234567\nAPP_SECRET=abcdef\n```\n\nHere's the example of your app:\n\n```go\npackage main\n\nimport (\n\t\"github.com/subosito/gotenv\"\n\t\"log\"\n\t\"os\"\n)\n\nfunc init() {\n\tgotenv.Load()\n}\n\nfunc main() {\n\tlog.Println(os.Getenv(\"APP_ID\"))     // \"1234567\"\n\tlog.Println(os.Getenv(\"APP_SECRET\")) // \"abcdef\"\n}\n```\n\nYou can also load other than `.env` file if you wish. Just supply filenames when calling `Load()`. It will load them in order and the first value set for a variable will win.:\n\n```go\ngotenv.Load(\".env.production\", \"credentials\")\n```\n\nWhile `gotenv.Load` loads entries from `.env` file, `gotenv.Apply` allows you to use any `io.Reader`:\n\n```go\ngotenv.Apply(strings.NewReader(\"APP_ID=1234567\"))\n\nlog.Println(os.Getenv(\"APP_ID\"))\n// Output: \"1234567\"\n```\n\nBoth `gotenv.Load` and `gotenv.Apply` **DO NOT** overrides existing environment variables. If you want to override existing ones, you can see section below.\n\n### Environment Overrides\n\nBesides above functions, `gotenv` also provides another functions that overrides existing:\n\n- `gotenv.OverLoad`\n- `gotenv.OverApply`\n\nHere's the example of this overrides behavior:\n\n```go\nos.Setenv(\"HELLO\", \"world\")\n\n// NOTE: using Apply existing value will be reserved\ngotenv.Apply(strings.NewReader(\"HELLO=universe\"))\nfmt.Println(os.Getenv(\"HELLO\"))\n// Output: \"world\"\n\n// NOTE: using OverApply existing value will be overridden\ngotenv.OverApply(strings.NewReader(\"HELLO=universe\"))\nfmt.Println(os.Getenv(\"HELLO\"))\n// Output: \"universe\"\n```\n\n### Throw a Panic\n\nBoth `gotenv.Load` and `gotenv.OverLoad` returns an error on something wrong occurred, like your env file is not exist, and so on. To make it easier to use, `gotenv` also provides `gotenv.Must` helper, to let it panic when an error returned.\n\n```go\nerr := gotenv.Load(\".env-is-not-exist\")\nfmt.Println(\"error\", err)\n// error: open .env-is-not-exist: no such file or directory\n\ngotenv.Must(gotenv.Load, \".env-is-not-exist\")\n// it will throw a panic\n// panic: open .env-is-not-exist: no such file or directory\n```\n\n### Another Scenario\n\nJust in case you want to parse environment variables from any `io.Reader`, gotenv keeps its `Parse` and `StrictParse` function as public API so you can use that.\n\n```go\n// import \"strings\"\n\npairs := gotenv.Parse(strings.NewReader(\"FOO=test\\nBAR=$FOO\"))\n// gotenv.Env{\"FOO\": \"test\", \"BAR\": \"test\"}\n\npairs, err := gotenv.StrictParse(strings.NewReader(`FOO=\"bar\"`))\n// gotenv.Env{\"FOO\": \"bar\"}\n```\n\n`Parse` ignores invalid lines and returns `Env` of valid environment variables, while `StrictParse` returns an error for invalid lines.\n\n## Notes\n\nThe gotenv package is a Go port of [`dotenv`](https://github.com/bkeepers/dotenv) project with some additions made for Go. For general features, it aims to be compatible as close as possible.\n","funding_links":[],"categories":["工具库","Utilities","Go","公用事业公司","工具库`可以提升效率的通用代码库和工具`","常用工具","Utility","实用工具"],"sub_categories":["查询语","Advanced Console UIs","Utility/Miscellaneous","交流","实用程序/Miscellaneous","爬虫工具","HTTP Clients","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","Fail injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsubosito%2Fgotenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsubosito%2Fgotenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsubosito%2Fgotenv/lists"}