Go dot env lib. Some of other go dot env library doesn't support go embed feature , so you can NOT embed the static file into go build executable binaries. This lib is a standard repo which support this feature, once you integrate with this repo, you can just simply run go build as usually we do, you don't need to care that you need to copy the static files to somewhere manually.
go get github.com/driftprogramming/godotenv@v1.1.0
see godotenv_test.go
in current repo Or see this:
.
├── envs.go // example here
├── envs # local, dev, test, live env files.
│ ├──.env # common env variables, which will be loaded in all other envs
│ ├──.env.dev # dev env variables
│ ├──.env.live # live env variables
│ ├──.env.local # local env variables
│ └──.env.test # test env variables
└── ...
// envs/.env.local
version=local.1.0
otherkey=abcd
// envs/.env.live
version=live.1.0
// envs.go
package root
import (
"embed"
"github.com/driftprogramming/godotenv"
)
//go:embed envs/*
var envs embed.FS
func SetupExample(env string) {
_ = godotenv.Load(envs, "envs/.env.live")
version1 := godotenv.Get("version") // version1 will be live.1.0
// the below method will not overwrite if env key already existed.
_ = godotenv.LoadWithoutOverwrite(envs, "envs/.env.local")
version2 := godotenv.Get("version") // version2 will still be live.1.0
_ = godotenv.Load(envs, "envs/.env.local")
version3 := godotenv.Get("version") // version3 will be local.1.0
}