dsoxcj7276 2018-11-19 10:22
浏览 242
已采纳

不使用“ go env”中指定的$ GOPATH

I've got

export GOPATH=$HOME/Documents/go

in my .zshrc.

When trying to build a project with the following:

sudo go build -o /usr/bin/

I get a

main.go:5:2: cannot find package "github.com/foo/bar" in any of:
    /usr/local/go/src/github.com/foo/bar (from $GOROOT)
    /Users/JoahJoah/go/src/github.com/foo/bar (from $GOPATH)

I am assuming the "(from $GOPATH)" means the path is whatever I've set my $GOPATH variable to. And

go env

outputs the following:

GOPATH="/Users/Johannes/Documents/go"

The "Documents" part of the path seems to be omitted when using go build.

Is build using a GOPATH from somewhere else?

  • 写回答

1条回答 默认 最新

  • doushi4795 2018-11-19 11:16
    关注

    Since I was running the the command

    $ sudo go build -o /usr/bin/
    

    with sudo prefixed, it was running as the root user, causing a different $GOPATH to be used.

    As Eric said, I should instead build to /usr/local/bin where sudo can be omitted.

    $ go build /usr/local/bin
    

    Works!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题