1 回答

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
這里的技巧是我.env在我的工作區(qū)文件夾下: ${workspaceFolder}/.env,但vscode沒(méi)有${workspaceFolder}用作我通過(guò)調(diào)用我的 go 函數(shù)來(lái)獲取文件的當(dāng)前工作目錄。.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)
}
所以,我只需要通過(guò)在我的文件中$cwd添加來(lái)設(shè)置當(dāng)前工作目錄:"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 關(guān)注
- 373 瀏覽
添加回答
舉報(bào)