{"id":15533443,"url":"https://github.com/datek/env","last_synced_at":"2025-10-15T09:21:31.848Z","repository":{"id":255436676,"uuid":"849861642","full_name":"DAtek/env","owner":"DAtek","description":"Library for parsing environmental variables into golang structs.","archived":false,"fork":false,"pushed_at":"2024-12-31T07:16:35.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-03T09:16:24.440Z","etag":null,"topics":["config","env","environment","environment-variables","golang"],"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/DAtek.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":"2024-08-30T11:55:49.000Z","updated_at":"2024-12-31T07:15:02.000Z","dependencies_parsed_at":"2024-08-31T05:21:17.060Z","dependency_job_id":null,"html_url":"https://github.com/DAtek/env","commit_stats":null,"previous_names":["datek/env"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DAtek%2Fenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DAtek%2Fenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DAtek%2Fenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DAtek%2Fenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DAtek","download_url":"https://codeload.github.com/DAtek/env/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246112638,"owners_count":20725300,"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":["config","env","environment","environment-variables","golang"],"created_at":"2024-10-02T11:37:03.240Z","updated_at":"2025-10-15T09:21:26.788Z","avatar_url":"https://github.com/DAtek.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![codecov](https://codecov.io/gh/DAtek/env/graph/badge.svg?token=MAjETYy681)](https://codecov.io/gh/DAtek/env) [![Go Report Card](https://goreportcard.com/badge/github.com/DAtek/env)](https://goreportcard.com/report/github.com/DAtek/env)\n\n\n# env\nLightweight library, which parses environmental variables into structs.\n\n## Features\n- Supports custom parsers\n- Optional fields\n- Default values\n- Generics\n- Returns friendly error message\n- Returned error can be parsed into struct for further processing if needed\n\n## Examples\n\n### Simple\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/DAtek/env\"\n)\n\ntype Config struct {\n\tAppMaxWorkers  int\n\tAppLoggingType *string\n}\n\nfunc main() {\n\tos.Clearenv()\n\tos.Setenv(\"APP_MAX_WORKERS\", \"10\")\n\n\tloadEnv := env.NewLoader[Config]()\n\tconfig, _ := loadEnv()\n\n\tfmt.Printf(\"config.AppMaxWorkers: %v\\n\", config.AppMaxWorkers)\n\tfmt.Printf(\"config.AppLoggingType: %v\\n\", config.AppLoggingType)\n}\n\n```\n\nOutput:\n```\nconfig.AppMaxWorkers: 10  \nconfig.AppLoggingType: \u003cnil\u003e\n```\n\n\n### Using defaults\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/DAtek/env\"\n)\n\ntype Config struct {\n\tAppMaxWorkers  int\n\tAppLoggingType *string\n}\n\ntype DefaultConfig struct {\n\tAppMaxWorkers int\n}\n\nfunc main() {\n\tos.Clearenv()\n\n\tloadEnv := env.NewLoader[Config]()\n\tdefaultConfig := DefaultConfig{AppMaxWorkers: 8}\n\tconfig, _ := loadEnv(defaultConfig)\n\n\tfmt.Printf(\"config.AppMaxWorkers: %v\\n\", config.AppMaxWorkers)\n\tfmt.Printf(\"config.AppLoggingType: %v\\n\", config.AppLoggingType)\n}\n\n```\n\nOutput:\n```\nconfig.AppMaxWorkers: 8\nconfig.AppLoggingType: \u003cnil\u003e\n```\n\n### Using custom parsers\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"github.com/DAtek/env\"\n)\n\ntype Point struct {\n\tX int\n\tY int\n}\n\ntype Config struct {\n\tAppCenter Point\n}\n\nvar customParsers = env.ParserMap{\n\t\"Point\": func(src string) (any, error) {\n\t\tparts := strings.Split(src, \";\")\n\t\tx, _ := strconv.Atoi(parts[0])\n\t\ty, _ := strconv.Atoi(parts[1])\n\n\t\treturn Point{x, y}, nil\n\t},\n}\n\nfunc main() {\n\tos.Clearenv()\n\tos.Setenv(\"APP_CENTER\", \"12;5\")\n\n\tloadEnv := env.NewLoader[Config](env.Config{Parsers: customParsers})\n\tconfig, _ := loadEnv()\n\n\tfmt.Printf(\"config.AppCenter: %v\\n\", config.AppCenter)\n}\n\n```\n\nOutput:\n```\nconfig.AppCenter: {12 5}\n```\n\n### Error\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/DAtek/env\"\n)\n\ntype Point struct {\n\tX int\n\tY int\n}\n\ntype Config struct {\n\tAppCenter     Point\n\tAppMaxWorkers int\n\tAppDbUrl      string\n}\n\nfunc main() {\n\tos.Clearenv()\n\tos.Setenv(\"APP_CENTER\", \"12;6\")\n\tos.Setenv(\"APP_MAX_WORKERS\", \"more than ever\")\n\n\tloadEnv := env.NewLoader[Config]()\n\t_, err := loadEnv()\n\n\tfmt.Println(err)\n\tfmt.Println()\n\n\terrorCollection := err.(*env.ErrorCollection)\n\tfmt.Printf(\"errorCollection: %v\\n\", errorCollection.Errors)\n}\n\n```\n\nOutput:\n```\nParser missing for environmental variable 'APP_CENTER'. Required type: 'Point'\nEnvironmental variable 'APP_MAX_WORKERS' has wrong type. Required type: 'int'\nEnvironmental variable 'APP_DB_URL' is unset\n\nerrorCollection: [{APP_CENTER unsupported_type Point PARSER_NOT_FOUND} {APP_MAX_WORKERS wrong_type int strconv.ParseInt: parsing \"more than ever\": invalid syntax} {APP_DB_URL required string \u003cnil\u003e}]\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatek%2Fenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdatek%2Fenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatek%2Fenv/lists"}