{"id":24196789,"url":"https://github.com/pchchv/env","last_synced_at":"2026-05-27T23:32:37.127Z","repository":{"id":176684032,"uuid":"620343908","full_name":"pchchv/env","owner":"pchchv","description":"Go library which loads environment variables from .env files.","archived":false,"fork":false,"pushed_at":"2023-04-07T19:05:47.000Z","size":55,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-01T00:48:50.173Z","etag":null,"topics":["dotenv","environment","environment-variables","go","golang","golang-library","golang-package"],"latest_commit_sha":null,"homepage":"","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/pchchv.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-28T13:53:11.000Z","updated_at":"2023-04-07T20:01:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"83ce8914-db8e-4c4c-acfd-06726562665e","html_url":"https://github.com/pchchv/env","commit_stats":null,"previous_names":["pchchv/env"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pchchv/env","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pchchv","download_url":"https://codeload.github.com/pchchv/env/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fenv/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33588345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-27T02:00:06.184Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","environment-variables","go","golang","golang-library","golang-package"],"created_at":"2025-01-13T19:38:08.135Z","updated_at":"2026-05-27T23:32:37.111Z","avatar_url":"https://github.com/pchchv.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **env** [![Go Reference](https://pkg.go.dev/badge/github.com/pchchv/golog.svg)](https://pkg.go.dev/github.com/pchchv/golog) [![Go Report Card](https://goreportcard.com/badge/github.com/pchchv/env)](https://goreportcard.com/report/github.com/pchchv/env)\nGo library which loads environment variables from .env files.  \nRuby project [dotenv](https://github.com/bkeepers/dotenv) port.\n\n\u003e Storing configuration in the environment is one of the tenets of a [twelve-factor app](https://12factor.net). 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 (to load into env for your own daemons, etc.) or as a bin command.\n\n## Installation\n\nAs a library\n\n```bash\ngo get github.com/pchchv/env\n```\n\nAs a bin command\n\ngo \u003e= 1.17\n```bash\ngo install github.com/pchchv/env/cmd/env@latest\n```\n\ngo \u003c 1.17\n```bash\ngo get github.com/pchchv/env/cmd/env\n```\n\n## Usage\n\nAdd your application configuration to your `.env` file in the root of your project:\n\n```bash\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/pchchv/env\"\n)\n\nfunc main() {\n  err := env.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\nAlso, you can simply take advantage of the autoload package which will read `.env` when importing\n\n```go\nimport _ \"github.com/pchchv/env/autoload\"\n```\n\nAlthough `.env` in the root of the project is used by default, you should not be restricted, both of the following examples are 100% acceptable\n\n```go\nenv.Load(\"somerandomfile\")\nenv.Load(\"filenumberone.env\", \"filenumbertwo.env\")\n```\n\nIf you want to be really fancy with your env file, you can make comments and export (below is the correct env file)\n\n```bash\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\nIf you don't want env to change your environment, you can just get the map back\n\n```go\nvar myEnv map[string]string\nmyEnv, err := env.Read()\n\ns3Bucket := myEnv[\"S3_BUCKET\"]\n```\n\nor from an `io.Reader` instead of a local file\n\n```go\nreader := getRemoteFile()\nmyEnv, err := env.Parse(reader)\n```\n\n... or from a `string` if you so desire\n\n```go\ncontent := getRemoteFileContent()\nmyEnv, err := env.Unmarshal(content)\n```\n\n### Precedence \u0026 Conventions\n\nExisting envs take precedence of envs that are loaded later.\n\n[convention](https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use)\nfor managing multiple environments (i.e. development, testing, production)\nis to create an environment named `{YOURAPP}_ENV` and load the environments in that order:\n\n```go\nenv := os.Getenv(\"FOO_ENV\")\nif \"\" == env {\n  env = \"development\"\n}\n\nenv.Load(\".env.\" + env + \".local\")\nif \"test\" != env {\n  env.Load(\".env.local\")\n}\nenv.Load(\".env.\" + env)\nenv.Load() // The Original .env\n```\n\nIf necessary, you can also use `env.Overload()` to break this convention and overwrite existing envs instead of just replacing them. Use with caution.\n\n### Command Mode\n\nSuppose you installed the command as above, and you have `$GOPATH/bin` in `$PATH`.\n\n```\nenv -f /some/path/to/.env some_command with some args\n```\n\nIf you do not specify `-f`, it will load `.env` into `PWD` by default.\n\nBy default it will not override existing environment variables; you can do this with the `-o` flag.\n\n### Writing Env Files\n\nenv can also write a map representing the environment to a correctly-formatted and escaped file\n\n```go\nenv, err := env.Unmarshal(\"KEY=value\")\nerr := env.Write(env, \"./.env\")\n```\n\nor to a string\n\n```go\nenv, err := env.Unmarshal(\"KEY=value\")\ncontent, err := env.Marshal(env)\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpchchv%2Fenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpchchv%2Fenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpchchv%2Fenv/lists"}