{"id":13367337,"url":"https://github.com/subosito/Gotenv","last_synced_at":"2025-03-12T18:32:26.252Z","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":"2023-08-15T12:33:43.000Z","size":113,"stargazers_count":284,"open_issues_count":0,"forks_count":35,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-13T21:55:03.448Z","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}},"created_at":"2013-08-27T12:56:47.000Z","updated_at":"2024-04-06T04:12:28.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":221309971,"owners_count":16795837,"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-07-30T00:01:44.901Z","updated_at":"2024-10-24T11:31:13.247Z","avatar_url":"https://github.com/subosito.png","language":"Go","funding_links":[],"categories":["实用工具","實用工具"],"sub_categories":["高级控制台界面","高級控制台界面"],"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","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"}