dotenv

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2024 License: MIT Imports: 3 Imported by: 0

README

dotenv

Go Library to read environment variables from .env file.

Tests Version AGPL License

Usage

install the library :

go get -u github.com/yassinebenaid/dotenv

.env

# 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

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

PORT=8080
HOST="example.com:$PORT"

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

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

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

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 for details.

Documentation

Overview

package "dotenv" provides functionality to consume .env files

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(override bool, files ...string) error

Load will read all environment variables from files and writes them to the ENV for this process. (typically using os.Setenv()).

  • if `override` is false, Load WILL NOT OVERRIDE the variables that are already set.
  • if `files` is empty, Read will default to loading .env in the current working directory.

you can pass multiple files like this :

dotenv.Load(false,"/path/to/file-1","/path/to/file-2")

in this case, Load will read them in the given order, and you can reference keys from other files , just like they were the same file.

file-1:

KEY_1=value

file-2:

KEY_2=$KEY_1

func Read

func Read(files ...string) (map[string]string, error)

Read reads all environment variables from the given files and return them as map.

  • if `files` is empty, Read will default to loading .env in the current working directory.

you can pass multiple files like this :

dotenv.Read("/path/to/file-1","/path/to/file-2")

in this case, Read will read them in the given order, and you can reference keys from other files , just like they were the same file.

file-1:

KEY_1=value

file-2:

KEY_2=$KEY_1

func Unmarshal

func Unmarshal(b []byte) (map[string]string, error)

Unmarshal reads environment variables from b and returns them as map.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL