Skip to content

Commit 20d77b3

Browse files
committed
Update doc
1 parent 2d3f6a1 commit 20d77b3

File tree

2 files changed

+116
-19
lines changed

2 files changed

+116
-19
lines changed

README.md

+55-12
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,59 @@
22

33
# env
44

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.
66

77
## Features
88

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.
1016

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.
1918

20-
In addition, additional methods for working with `.env` files and data exchange between environment variables and the Go structs are implemented.
19+
## Installation
2120

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+
```
2358

2459
## Env-file supports
2560

@@ -583,3 +618,11 @@ func main() {
583618
// Allowed hosts: [localhost 127.0.0.1]
584619
}
585620
```
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.

doc.go

+61-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,62 @@
1-
// Package env provides a variety of methods for managing and streamlining
2-
// environment variables. It supports loading data from .env files into the
3-
// environment and offers seamless data transfer between the environment and
4-
// custom data structures, allowing for effortless updates to structure fields
5-
// from environment variables or vice versa, set environment variables from
6-
// structure fields. The env package supports all standard data types, as well
7-
// as the url.URL type.
1+
// Package env provides a comprehensive solution for managing environment
2+
// variables in Go applications. It offers a rich set of features for handling
3+
// environment configuration through both .env files and runtime environment
4+
// variables.
5+
//
6+
// Core Features:
7+
// - Concurrent parsing of .env files with configurable parallelism
8+
// - Bidirectional mapping between environment variables and Go structures
9+
// - Support for nested structures and complex data types
10+
// - Advanced type conversion with validation
11+
// - URL parsing and validation support
12+
// - Flexible prefix-based filtering
13+
// - Custom marshaling and unmarshaling interfaces
14+
//
15+
// The package supports loading configuration from .env files with features like:
16+
// - Variable expansion (${VAR} or $VAR syntax)
17+
// - Quoted values with escape sequences
18+
// - Comments and inline comments
19+
// - Export statements
20+
// - Multi-line values
21+
// - Default values
22+
// - Custom separators for arrays/slices
23+
//
24+
// Type Support:
25+
// The package handles all common Go types including:
26+
// - Basic types: string, bool, int/uint (all sizes), float32/64
27+
// - Complex types: url.URL, custom structs
28+
// - Collections: arrays, slices
29+
// - Nested structures with automatic prefix handling
30+
// - Pointers to supported types
31+
//
32+
// Structure Tags:
33+
// - env: specifies the environment variable name
34+
// - def: provides default values
35+
// - sep: defines separator for array/slice values
36+
//
37+
// Example usage:
38+
//
39+
// type Config struct {
40+
// Host string `env:"HOST" def:"localhost"`
41+
// Port int `env:"PORT" def:"8080"`
42+
// IPs []string `env:"ALLOWED_IPS" sep:","`
43+
// APIUrl url.URL `env:"API_URL"`
44+
// }
45+
//
46+
// func main() {
47+
// var cfg Config
48+
// // Load .env file and parse it
49+
// if err := env.Load(".env"); err != nil {
50+
// log.Fatal(err)
51+
// }
52+
// // Map environment variables to structure
53+
// if err := env.Unmarshal("", &cfg); err != nil {
54+
// log.Fatal(err)
55+
// }
56+
// }
57+
//
58+
// The package is designed to be efficient and safe, with careful handling of
59+
// concurrent operations and proper error management. It provides a clean API
60+
// that follows Go idioms while offering powerful features for complex
61+
// configuration scenarios.
862
package env

0 commit comments

Comments
 (0)