{"id":23343248,"url":"https://github.com/vinukathejana/env","last_synced_at":"2025-07-27T00:09:26.660Z","repository":{"id":253930320,"uuid":"844964719","full_name":"VinukaThejana/env","owner":"VinukaThejana","description":"A simple and flexible Go package for loading environment variables from a config file or the system environment and unmarshaling them into a struct. Automatically supports multiple data types with clear error handling.","archived":false,"fork":false,"pushed_at":"2024-08-20T13:52:09.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T16:19:57.289Z","etag":null,"topics":["environment-variables","golang","parser"],"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/VinukaThejana.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2024-08-20T10:10:23.000Z","updated_at":"2024-08-20T13:42:59.000Z","dependencies_parsed_at":"2024-08-20T12:23:26.234Z","dependency_job_id":"c2621608-b1c8-410e-93e8-82264fbc8d9e","html_url":"https://github.com/VinukaThejana/env","commit_stats":null,"previous_names":["vinukathejana/env"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/VinukaThejana/env","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VinukaThejana%2Fenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VinukaThejana%2Fenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VinukaThejana%2Fenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VinukaThejana%2Fenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VinukaThejana","download_url":"https://codeload.github.com/VinukaThejana/env/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VinukaThejana%2Fenv/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267273245,"owners_count":24062563,"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","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"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":["environment-variables","golang","parser"],"created_at":"2024-12-21T06:13:40.819Z","updated_at":"2025-07-27T00:09:26.612Z","avatar_url":"https://github.com/VinukaThejana.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Env\n\nThe Env package provides a generic interface for loading environment variables from a config file (such as .env) and unmarshaling them into a Go struct. If a config file is not provided, the package will attempt to load environment variables directly from the system environment. In case of an error while loading the environment variables, the program will panic with a clear indication of the error.\n\n## Features\n\n- Load environment variables from:\n    - System environment variables\n    - `.env` files\n    - Custom file paths\n\n- Unmarshal environment variables into structs\n\n- Support for string and integer types\n\n- Customize configuration file name and path\n\n- Validation of the loaded configuration\n\n## Installation\n\nTo install go get\n\n```bash\ngo get github.com/VinukaThejana/env\n```\n\n## Usage\n\nHere's a quick example of how to use `Env`\n\n```go\n// Package env is a generic package that can be used to load environment variables \n// from a config file or system environment variables\npackage env\n\nimport (\n    \"fmt\"\n    environ \"github.com/VinukaThejana/env\"\n)\n\n// Env is a struct that contains the environment variables\ntype Env struct {\n    DatabaseURL string `mapstructure:\"DATABASE_URL\"`\n    Port        int    `mapstructure:\"PORT\"`\n}\n\n// Load loads the environment variables from the config file or system environment variables\nfunc (e *Env)Load(path ...string) {\n    environ.Load(e, path...)\n}\n```\n\n## Loading Environment Variables\n\nEnv provides several ways to load environment variables:\n\n1. __From system environment variables__ if no .env file if found, Env automatically falls back to loading from the system environment variables.\n\n2. __From `.env` file in the current directory__ By default, Env looks for a `.env` file in the current directory:\n```go\nenviron.Load(e)\n```\n\n3. __from custom path__  you can sepcify a custom path for your configuration file:\n```go\nenviron.Load(e, \"/custom/path/to/.env/file\")\n```\n\n4. __With custom file name__ You can also specify both the custom path and a custom file name:\n```go\nenviron.Load(e, \"/custom/path/to/.env/file\", \"custom_file_name\")\n```\n\n## Configuring the struct\n\nYour configuration should use the `mapstructure` tag to map the environment variables to the struct fields:\n\n```go\ntype Env struct {\n    DatabaseURL string `mapstructure:\"DATABASE_URL\"`\n    Port        int    `mapstructure:\"PORT\"`\n    Debug       bool   `mapstructure:\"DEBUG\"`\n}\n```\n\n## Supported Types\n\nEnv currently supports the following types for struct fields:\n\n- `string`\n- `int`, `int8`, `int16`, `int32`, `int64`\n- `float32`, `float64`\n- `bool`\n- `time.Time` (parsed using RFC3339 format)\n\n## Error handling\n\nEnv uses the `github.com/VinukaThejana/go-utils/logger` package for error logging. If an error occurs during loading or parsing, it will be logged, and the program will exit.\n\n## Validation\n\nAfter loading the configuration, Env automatically uses the `validate` tag if present in the struct and uses `go-playground/validator` for validating the struct fields. If there is an error the program will quit.\n\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nEnv is released under the MIT License.\n\n## Acknowledgements\n\nThis package uses the following third-party libraries:\n- github.com/spf13/viper\n- github.com/go-playground/validator\n- github.com/VinukaThejana/go-utils/logger\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinukathejana%2Fenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvinukathejana%2Fenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinukathejana%2Fenv/lists"}