err := godotenv.Load(".env")
if err != nil { panic(err.Error()) }
shell := os.Getenv("SHELL")
fmt.Println(shell)
Run Code Online (Sandbox Code Playgroud)
SHELL=/bin/zsh我在文件中设置了.env,但操作系统似乎首先在操作系统环境变量列表中查找给定的键,然后检查该.env文件。有没有办法将这两者分开?
小智 5
是的,有办法解决这个问题。有github.com/joho/godotenv一个名为 的函数Read()。您可以将 .env 文件加载到地图数据结构中。
envFile, _ := godotenv.Read(".env")
envFileShell = envFile["SHELL"]
fmt.Println(envFileShell) // will be /bin/zsh (what you set in .env file)
osShell := os.Getenv("SHELL")
fmt.Println(osShell) // will be whatever it is set in your operating system
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8083 次 |
| 最近记录: |