1 回答
TA贡献1880条经验 获得超4个赞
这里的技巧是我.env在我的工作区文件夹下: ${workspaceFolder}/.env,但vscode没有${workspaceFolder}用作我通过调用我的 go 函数来获取文件的当前工作目录。.envos.Getwd()
load_eng.go摘录如下:
curDir, err := os.Getwd()
if err != nil {
log.Println(err)
}
loadErr := godotenv.Load(curDir + "/.env")
if loadErr != nil {
log.Fatalln("can't load env file from current directory: " + curDir)
}
所以,我只需要通过在我的文件中$cwd添加来设置当前工作目录:"cwd": "${workspaceFolder}"launch.json
"configurations": [
{
"name": "My Application",
"type": "go",
"request": "launch",
"mode": "debug",
"envFile": "${workspaceFolder}/.env",
"program": "${workspaceFolder}/src/main.go",
"cwd": "${workspaceFolder}"
}
]
有用的资源:
https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables
https://github.com/golang/vscode-go/wiki/debugging
- 1 回答
- 0 关注
- 256 浏览
添加回答
举报