Go Dotenv is a lightweight Go package designed to simplify the configuration of your Go applications by loading environment variables from a .env
file (or any file of your choice). It provides a flexible structure, allowing you to customize the configuration process based on your application's needs.
Note: The default env
file is a ".env" file at the root of the project and the default logger
writes to os.std.Out
- Simple Configuration: Easily load environment variables from a specified .env file or use default values.
- Logger Integration: Seamlessly integrate with your existing logging infrastructure by accepting a
*log.Logger
as a parameter. - Error Handling: Provides clear error messages for quick debugging.
- Customization: Customize the configuration process by specifying a filename, logger, or both, depending on your application requirements.
go get -u github.com/tonie-ng/go-dotenv
There are various ways to use this package.
- Using the default values for the filename and logger. Please ensure to have a
.env
file at the root directory of your project
package main
import (
"fmt"
"os"
"github.com/tonie-ng/go-dotenv"
)
func main() {
// loads the env variables using the defaults while ignoring the return values.
dotenv.Config()
envVariable := os.Getenv("MY_ENV_KEY")
// This prints the value assigned to the MY_ENV_KEY
fmt.Println(envVariable)
}
or
package main
import (
"fmt"
"github.com/tonie-ng/go-dotenv"
)
func main() {
// dotenv.Config() returns a map of all the environment variables provided in the .env file and an err if any.
envVars, err := dotenv.Config()
if err != nil {
fmt.Println(err)
}
for _, v := range envVars {
fmt.Println(v)
}
}
- Providing a value for either the logger or the filename. (If one is provided the other is assigned to the default.)
package main
import (
"fmt"
"log"
"os"
"github.com/tonie-ng/go-dotenv"
)
func main() {
// We provide a string here and this is assigned to the filename while the logger uses the dafault.
dotenv.Config("env")
// To assign the logger to a value instead, we do:
logger := log.New(os.Stdout, "Example logger", log.LstdFlags)
dotenv.Config(logger)
// You can also assign logger to nil by doing. This indicates that you dont want a logger.
dotenv.Config(nil)
// prints the value of the MY_ENV_KEY provided in the env file
fmt.Println(os.Getenv("MY_ENV_KEY"))
}
- Providing a logger and a filename.
package main
import (
"fmt"
"log"
"os"
"github.com/tonie-ng/go-dotenv"
)
func main() {
filename := "sample.env"
logger := log.New(os.Stdout, "Example logger", log.LstdFlags)
dotenv.Config(filename, logger)
// You can also assign logger to nil by doing. This indicates that you don't want a logger.
dotenv.Config(filename, nil)
fmt.Println(os.Getenv("MY_ENV_KEY"))
}
Contributions to this project are welcome, plese refer to the contibution guide.
- Add support for string recognition in env files. ie
TEST_ENV="envvarwithstrings" should output => envvarwithstrings but without the double quotes
Note: I should probably raise issues instead of updating this TODO list :/.