为了账号安全,请及时绑定邮箱和手机立即绑定

在 vscode 中未检测到 env 文件

在 vscode 中未检测到 env 文件

Go
胡说叔叔 2023-01-03 16:13:44
我有一个my-project多根工作区:- my-project   - my-project-api-gateway   - another-root-folder-we-dont-care-about这是我的launch.json配置文件my-project-api-gateway{  // See https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes  // and variables here https://code.visualstudio.com/docs/editor/variables-reference  "version": "0.2.0",  "configurations": [    {      "name": "my-project-api-gateway",      "type": "go",      "request": "launch",      "mode": "debug",      "envFile": "${workspaceFolder}/.env", ----> Error loading .env file      "program": "${workspaceFolder}/src/main.go"    }  ]}调试控制台输出Starting: /Users/me/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:53890 --log-dest=3 from /Users/me/path/to/project/srcDAP server listening at: 127.0.0.1:53890Type 'dlv help' for list of commands.2022/07/20 10:30:52 Error loading .env fileProcess 13526 has exited with status 1Detachingdlv dap (13508) exited with code: 0谢谢你的帮助dlv、go、gopls 和 vscode 版本❯ dlv versionDelve DebuggerVersion: 1.8.2❯ go versiongo version go1.18 darwin/amd64❯ gopls -v versionBuild info----------golang.org/x/tools/gopls v0.9.1❯ code -v1.69.2扩展Go for Visual Studio Code是 v0.35.1
查看完整描述

1 回答

?
慕村225694

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


查看完整回答
反对 回复 2023-01-03
  • 1 回答
  • 0 关注
  • 256 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信