Skip to content

XxThunderBlastxX/goconfigenv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoConfigEnv

GoDoc

GoConfigEnv is a Go library that allows you to easily load configuration values from environment variables and map them to a struct.

💉 Installation

  go get -u github.com/XxThunderBlastxX/goconfigenv

🛠️ Usage

Add all your application configuration in your .env file:

  PORT=8080
  HOST=localhost
  DEBUG=true

Create a struct that represents your configuration and just simply call the function ParseEnv and pass the struct type as its generic type. The library will automatically map the values from the environment variables and return the struct. :

  package main

  import (
    "fmt"
    "github.com/XxThunderBlastxX/goconfigenv"
  )

  type Config struct {
    Port  int    `env:"PORT" `
    Host  string `env:"HOST"`
    Debug bool   `env:"DEBUG"`
  }

  func main() {
    config, err := goconfigenv.ParseEnv[Config]()
    if err != nil {
      fmt.Println(err)
    }

	fmt.Println(config.Port)
	fmt.Println(config.Host)
	fmt.Println(config.Debug)
  }

You can also configure your tags by setting the default value to the field. It takes the default value if the environment variable is not set:

  type Config struct {
    Port  int    `env:"PORT,default=8080"`
    Host  string `env:"HOST,default=localhost"`
    Debug bool   `env:"DEBUG,default=true"`
  }

By default it takes the values from .env file at root of the dir. If you want to change this behaviour you can set the custom path and file location using godotenv

    err := godotenv.Load("path/to/your/.env")

📄 License

This project is licensed under the MIT License - see the LICENCE file for details.

🤝 Show your support

Give a ⭐️ if this project helped you!

🤝 Contribution

Contributions, issues and feature requests are always welcome!

Feel free to check issues page.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages