https://github.com/yassinebenaid/dotenv
Go Library to read environment variables from .env file.
https://github.com/yassinebenaid/dotenv
environment-variables go library
Last synced: 12 months ago
JSON representation
Go Library to read environment variables from .env file.
- Host: GitHub
- URL: https://github.com/yassinebenaid/dotenv
- Owner: yassinebenaid
- License: mit
- Created: 2024-02-08T18:46:09.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-01T10:12:49.000Z (over 2 years ago)
- Last Synced: 2025-03-26T15:54:35.586Z (over 1 year ago)
- Topics: environment-variables, go, library
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# dotenv
Go Library to read environment variables from .env file.
[](https://github.com/yassinebenaid/dotenv/actions/workflows/tests.yaml)
[](https://badge.fury.io/gh/yassinebenaid%2Fdotenv)
[](./LICENCE)
# Usage
install the library :
```bash
go get -u github.com/yassinebenaid/dotenv
```
_.env_
```bash
# comments are supported
PORT=8080
HOST="example.com:$PORT" # inline comments too
```
then in the beginning of your program, preferrably in the `main` function:
_main.go_
```go
package main
import (
"os"
"fmt"
"github.com/yassinebenaid/dotenv"
)
func main(){
err := dotenv.Load(false,".env")
if err != nil{
panic(err)
}
fmt.Println(os.Getenv("PORT")) // 8080
fmt.Println(os.Getenv("HOST")) // example.com:8080
// if no file path provided, Load will default
// to loading .env in current working directory
err = dotenv.Load(false)
if err != nil{
panic(err)
}
fmt.Println(os.Getenv("PORT")) // 8080
fmt.Println(os.Getenv("HOST")) // example.com:8080
// ...
}
``
```
# API
## dotenv.Load
_.env_
```bash
PORT=8080
HOST="example.com:$PORT"
```
_main.go_
```go
package main
import (
"os"
"fmt"
"github.com/yassinebenaid/dotenv"
)
func main(){
os.Setenv("PORT","5000")
err := dotenv.Load(false) // will not override
if err != nil{
panic(err)
}
fmt.Println(os.Getenv("PORT")) // 5000
// if you want to override
os.Setenv("PORT","5000")
err = dotenv.Load(true) // will override
if err != nil{
panic(err)
}
fmt.Println(os.Getenv("PORT")) // 8080
// you can pass multiple files
err = dotenv.Load(true,"./path/to/file-1","./path/to/file-2")
if err != nil{
panic(err)
}
}
``
```
## dotenv.Read
_main.go_
```go
package main
import (
"fmt"
"github.com/yassinebenaid/dotenv"
)
func main() {
env, err := dotenv.Read("/path/to/.env")
if err != nil {
panic(err)
}
fmt.Println(env["PORT"])
// you can pass multiple files
env, err = dotenv.Read("/path/to/.env-1", "/path/to/.env-2")
if err != nil {
panic(err)
}
// if not path provided, Read will default to loading .env in current
// working directory
env, err = dotenv.Read()
if err != nil {
panic(err)
}
}
``
```
`
## dotenv.Unmarshal
_main.go_
```go
package main
import (
"fmt"
"github.com/yassinebenaid/dotenv"
)
func main(){
input := []byte(`
KEY_1=value
KEY_2=value
`)
env,err := dotenv.Unmarshal(input)
if err != nil{
panic(err)
}
fmt.Println(env["KEY_1"])
}
``
```
for more details check [go doc](https://pkg.go.dev/pkg/github.com/yassinebenaid/dotenv)
# Contributing
Contributions are welcome, make sure to include tests or update exising ones to cover your changes.
_code changes without tests and references to peer dotenv implementations will not be accepted_
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Added some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
# Licence
Check the [Licence file](./LICENCE) for details.