doubi1797 2017-06-13 21:26
浏览 616
已采纳

Gopath环境错误

I'm using Visual Studio code to write some go code. Everything was working fine yesterday, but now I can't run the debugger or build in VS-Code.

I am on Windows 10, and I use Powershell as my terminal of choice.

I get the following error:

go: GOPATH entry is relative; must be absolute path: "/Users/efronlicht/go".
For more details see: 'go help gopath'
exit status 2
Process exiting with code: 1

This is a VS-CODE specific error, because I can build go source files with go build through the terminal as usual.

Here are the results of go env:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\work\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

As you can see, my GOPATH is an absolute path, not a relative one.

  • 写回答

2条回答 默认 最新

  • douxian1923 2017-06-17 04:30
    关注

    I use VSCode 1.13.1 on Windows 10, and I launch or debug without any issue.

    Launching involves in your workspace a ${workspaceroot}/.vscode/tasks.json file.
    To be sure of the GOPATH value, mine includes:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "0.1.0",
        "command": "build",
        "isShellCommand": true,
        "showOutput": "always",
        "tasks": [
            {
                "options": {
                    "env": {
                        "GOROOT": "D:/prgs/go/latest",
                        "GOPATH": "${workspaceRoot}"
                    }
                },
                "echoCommand": false,
                "taskName": "install",
                "isBuildCommand": true
            },
    

    You can replace "${workspaceRoot}" by C:/work/go in your case.
    That way, a <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> triggers a compilation+installation (go install)

    And the debugger involves:

    • having a dlv.exe in the %PATH%
    • a ${workspaceroot}/.vscode/launch.json file

    Here is mine

    {
        "version": "0.2.0",
        "configurations": [
            {
                "stopOnEntry": false,
                "cwd": "${workspaceRoot}",
                "name": "Launch",
                "type": "go",
                "request": "launch",
                "mode": "debug",
                "remotePath": "",
                "port": 2345,
                "host": "127.0.0.1",
                "program": "${fileDirname}",
                "env": {
                    "GOPATH": "${workspaceRoot}"
                },
                "args": [],
                "showLog": true
            }
        ]
    }
    

    Again you can replace "${workspaceRoot}" by C:/work/go in your case (both in GOPATH and cwd).

    Note that I specificy GOPATH as well as cwd (current working directory) I open my file relative from the workspace root (that way, the breakpoints are recognized). A simple <kbd>F5</kbd> from main.go does run delve successfully (on Windows!)

    With that, I can launch VSCode from a cmd Windows shell which has no GOROOT or GOPATH set, and it still works. (because my local user settings do include "go.goroot": "D:/prgs/go/latest")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型