|
2 | 2 |
|
3 | 3 | # env
|
4 | 4 |
|
5 |
| -The env package provides a variety of methods for managing environment variables. It supports loading data from `.env` files into the environment and provides data transfer between the environment and custom Go data structures, allowing you to effortlessly update structure fields from environment variables or vice versa, set environment variables from Go structure fields. The env package supports all standard Go data types (strings, numbers, boolean expressions, slices, arrays, etc.), as well as the complex `url.URL` type. |
| 5 | +A powerful and flexible environment variable management package for Go with support for `.env` files, struct mapping, and advanced type conversion. It supports loading data from `.env` files into the environment and provides data transfer between the environment and custom Go data structures, allowing you to effortlessly update structure fields from environment variables or vice versa, set environment variables from Go structure fields. The env package supports all standard Go data types (strings, numbers, boolean expressions, slices, arrays, etc.), as well as the complex `url.URL` type. |
6 | 6 |
|
7 | 7 | ## Features
|
8 | 8 |
|
9 |
| -The main features of the env module include: |
| 9 | +- **Concurrent Processing**: Parse `.env` files using configurable parallel processing. |
| 10 | +- **Bi-directional Mapping**: Convert between environment variables and Go structures. |
| 11 | +- **Rich Type Support**: Handle all Go basic types, arrays, slices, and custom types. |
| 12 | +- **Advanced Parsing**: Support for variable expansion, quotes, comments, and multi-line values. |
| 13 | +- **Prefix Filtering**: Filter environment variables by prefix. |
| 14 | +- **Custom Interfaces**: Implement custom marshaling/unmarshaling behavior. |
| 15 | +- **Production Ready**: Thread-safe operations and comprehensive error handling. |
10 | 16 |
|
11 |
| - - setting environment variables from variables defined in an env-file; |
12 |
| - - converting (marshaling) a Go structure to environment variables; |
13 |
| - - extracting (unmarshaling) environment variables to a Go structure; |
14 |
| - - setting any variables to the environment; |
15 |
| - - deleting variables from the environment; |
16 |
| - - checking for the presence of a variable in the environment; |
17 |
| - - retrieving the value of a variable by key from the environment; |
18 |
| - - clearing the environment. |
| 17 | +The module provides synonyms for the standard methods from the os module, such as `Get` for `os.Getenv`, `Set` for `os.Setenv`, `Unset` for `os.Unsetenv`, `Clear` for `os.Clearenv`, and Environ for `os.Environ`, to manage the environment. Additionally, it implements custom methods that enable saving variables from the environment into structures. |
19 | 18 |
|
20 |
| -In addition, additional methods for working with `.env` files and data exchange between environment variables and the Go structs are implemented. |
| 19 | +## Installation |
21 | 20 |
|
22 |
| -The module provides synonyms for the standard methods from the os module, such as `Get` for `os.Getenv`, `Set` for `os.Setenv`, `Unset` for `os.Unsetenv`, `Clear` for `os.Clearenv`, and Environ for `os.Environ`, to manage the environment. Additionally, it implements custom methods that enable saving variables from the environment into structures. |
| 21 | +```bash |
| 22 | +go get -u github.com/goloop/env |
| 23 | +``` |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +```go |
| 28 | +package main |
| 29 | + |
| 30 | +import ( |
| 31 | + "fmt" |
| 32 | + "github.com/goloop/env" |
| 33 | + "log" |
| 34 | +) |
| 35 | + |
| 36 | +type Config struct { |
| 37 | + Host string `env:"HOST" def:"localhost"` |
| 38 | + Port int `env:"PORT" def:"8080"` |
| 39 | + IPs []string `env:"ALLOWED_IPS" sep:","` |
| 40 | + IsDebug bool `env:"DEBUG" def:"true"` |
| 41 | +} |
| 42 | + |
| 43 | +func main() { |
| 44 | + // Load .env file. |
| 45 | + if err := env.Load(".env"); err != nil { |
| 46 | + log.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + // Parse environment into struct. |
| 50 | + var cfg Config |
| 51 | + if err := env.Unmarshal("", &cfg); err != nil { |
| 52 | + log.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + fmt.Printf("%+v\n", cfg) |
| 56 | +} |
| 57 | +``` |
23 | 58 |
|
24 | 59 | ## Env-file supports
|
25 | 60 |
|
@@ -583,3 +618,11 @@ func main() {
|
583 | 618 | // Allowed hosts: [localhost 127.0.0.1]
|
584 | 619 | }
|
585 | 620 | ```
|
| 621 | + |
| 622 | +## Contributing |
| 623 | + |
| 624 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 625 | + |
| 626 | +## License |
| 627 | + |
| 628 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments