求助,导入包时不检索 GOPATH 下的包

问题描述

  • $GOPATH/src中新建了一个demo包
  • 在执行 go run try.go 时报 package demo is not in GOROOT (D:\Go\src\demo)

这是try.go的文件

导入包时不检索GOPATH下的包   package demo is not in GOROOT (D:\Go\src\demo)

这是test.go的文件

导入包时不检索GOPATH下的包   package demo is not in GOROOT (D:\Go\src\demo)

env配置

导入包时不检索GOPATH下的包   package demo is not in GOROOT (D:\Go\src\demo)

所做的尝试

  • 我将demo包放在 $GOROOT/src 时运行正常
  • 在经过搜索之后了解到import的导入过程

导入包时不检索GOPATH下的包   package demo is not in GOROOT (D:\Go\src\demo)

按照文中所说我这样是没有问题的,可是我在 $GOPATH 下的包并没有被检索

最后

希望各位老哥指个路,谢谢各位

最佳答案

goland有独立的env配置,你设置goland的配置才行,goland的gopath应该是空。

我build一个不存在的包xxx,先在goroot然后在多个gopath查找的包,所以仅提示goroot没有就是没有gopath。

[root@localhost _example]# go build xxxx
can't load package: package xxxx: cannot find package "xxxx" in any of:
    /usr/local/go1.10/src/xxxx (from $GOROOT)
    /root/go/src/xxxx (from $GOPATH)
    /mnt/hgfs/gopath/src/xxxx
    /data/web/golang/src/xxxx

goland设置打开“File->settings”,弹出如下配置界面:

Go

还有一个需要注意的地方,环境变量GO111MODULE是gomod的开关,on启用,off关闭,auto在gopath内就是启用之外是关闭,设置on值可能有问题,我自己并不使用gomod。

3年前 评论
讨论数量: 10

goland有独立的env配置,你设置goland的配置才行,goland的gopath应该是空。

我build一个不存在的包xxx,先在goroot然后在多个gopath查找的包,所以仅提示goroot没有就是没有gopath。

[root@localhost _example]# go build xxxx
can't load package: package xxxx: cannot find package "xxxx" in any of:
    /usr/local/go1.10/src/xxxx (from $GOROOT)
    /root/go/src/xxxx (from $GOPATH)
    /mnt/hgfs/gopath/src/xxxx
    /data/web/golang/src/xxxx

goland设置打开“File->settings”,弹出如下配置界面:

Go

还有一个需要注意的地方,环境变量GO111MODULE是gomod的开关,on启用,off关闭,auto在gopath内就是启用之外是关闭,设置on值可能有问题,我自己并不使用gomod。

3年前 评论

goland有独立的env配置,你设置goland的配置才行,goland的gopath应该是空。

我build一个不存在的包xxx,先在goroot然后在多个gopath查找的包,所以仅提示goroot没有就是没有gopath。

[root@localhost _example]# go build xxxx
can't load package: package xxxx: cannot find package "xxxx" in any of:
    /usr/local/go1.10/src/xxxx (from $GOROOT)
    /root/go/src/xxxx (from $GOPATH)
    /mnt/hgfs/gopath/src/xxxx
    /data/web/golang/src/xxxx

goland设置打开“File->settings”,弹出如下配置界面:

Go

还有一个需要注意的地方,环境变量GO111MODULE是gomod的开关,on启用,off关闭,auto在gopath内就是启用之外是关闭,设置on值可能有问题,我自己并不使用gomod。

3年前 评论

@1112

  • goland 已经配置过了 file
  • 我在cmd中运行也是同样的结果

file

3年前 评论

file
勾选这个没有? goland 要开启 Module 才行的

3年前 评论

@1112 果然如老哥所说,我在将GO111MODULE设置为off之后编译正常了,多谢老哥。下班回家研究研究这是为什么

3年前 评论

因为gomod和gopath两个包管理方案,并且相互不兼容有他没我那样。

在gopath查找包,按照goroot和多gopath目录下src/xxx依次查找。

在gomod下查找包,解析go.mod文件查找包,mod包名就是包的前缀,里面的目录就后续路径了。

在gomod模式下,查找包就不会去gopath查找,只是gomod包缓存在gopath/pkg/mod里面。

3年前 评论
kenny_xiao 1年前

还有一种可能,看看是不是goland 的版本太老了。我的是2018.1的,这个时候ide对go module 支持不是很好,采用go mod引本地包的话,ide的build 没法用,换成2020.1版就可以了。(前提条件是你在命令行里面能跑,排除其他的问题)。

3年前 评论

我按照上面做了,GO111MODULE也设置为 off 了,还是没有解决,有别的方法吗

3年前 评论
lidongyoo (楼主) 3年前
cky91 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!