在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:joho/godotenv开源软件地址:https://github.com/joho/godotenv开源编程语言:Go 98.1%开源软件介绍:GoDotEnvA Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file). From the original Library:
It can be used as a library (for loading in env for your own daemons etc.) or as a bin command. There is test coverage and CI for both linuxish and Windows environments, but I make no guarantees about the bin version working on Windows. InstallationAs a library go get github.com/joho/godotenv or if you want to use it as a bin command go >= 1.17 go install github.com/joho/godotenv/cmd/godotenv@latest go < 1.17 go get github.com/joho/godotenv/cmd/godotenv UsageAdd your application configuration to your S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE Then in your Go app you can do something like package main
import (
"github.com/joho/godotenv"
"log"
"os"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
s3Bucket := os.Getenv("S3_BUCKET")
secretKey := os.Getenv("SECRET_KEY")
// now do something with s3 or whatever
} If you're even lazier than that, you can just take advantage of the autoload package which will read in import _ "github.com/joho/godotenv/autoload" While _ = godotenv.Load("somerandomfile")
_ = godotenv.Load("filenumberone.env", "filenumbertwo.env") If you want to be really fancy with your env file you can do comments and exports (below is a valid env file) # I am a comment and that is OK
SOME_VAR=someval
FOO=BAR # comments at line end are OK too
export BAR=BAZ Or finally you can do YAML(ish) style FOO: bar
BAR: baz as a final aside, if you don't want godotenv munging your env you can just get a map back instead var myEnv map[string]string
myEnv, err := godotenv.Read()
s3Bucket := myEnv["S3_BUCKET"] ... or from an reader := getRemoteFile()
myEnv, err := godotenv.Parse(reader) ... or from a content := getRemoteFileContent()
myEnv, err := godotenv.Unmarshal(content) Precedence & ConventionsExisting envs take precedence of envs that are loaded later. The convention
for managing multiple environments (i.e. development, test, production)
is to create an env named env := os.Getenv("FOO_ENV")
if "" == env {
env = "development"
}
godotenv.Load(".env." + env + ".local")
if "test" != env {
godotenv.Load(".env.local")
}
godotenv.Load(".env." + env)
godotenv.Load() // The Original .env If you need to, you can also use Command ModeAssuming you've installed the command as above and you've got
If you don't specify Writing Env FilesGodotenv can also write a map representing the environment to a correctly-formatted and escaped file env, err := godotenv.Unmarshal("KEY=value")
err := godotenv.Write(env, "./.env") ... or to a string env, err := godotenv.Unmarshal("KEY=value")
content, err := godotenv.Marshal(env) ContributingContributions are welcome, but with some caveats. This library has been declared feature complete (see #182 for background) and will not be accepting issues or pull requests adding new functionality or breaking the library API. Contributions would be gladly accepted that:
code changes without tests and references to peer dotenv implementations will not be accepted
ReleasesReleases should follow Semver though the first couple of releases are Use annotated tags for all releases. Example Who?The original library dotenv was written by Brandon Keepers, and this port was done by John Barton based off the tests/fixtures in the original library. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论