英文:
How to solve the import-local-package problem: GOPATH is ignored, only GOROOT takes effects
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是GO的新手,在环境配置上卡了几个小时。
我成功运行了一些测试项目,但是当我尝试导入我的自定义本地包(比如xxx)时,"go run"命令失败了,并记录了以下错误信息:
test/main/main.go:6:2: package test/xxx is not in GOROOT (/usr/local/go/src/test/xxx)
在我的Ubuntu电脑上,奇怪的是当导入本地包时,似乎忽略了GOPATH。
go版本是go1.16.5 linux/amd64
GOPATH是通过*export GOPATH="$HOME/GoProjects"*设置的
GOROOT是通过*export GOPATH="/usr/local/go"*设置的
使用go env -w GO111MODULE=on打开了GoMod
在编辑项目的.go文件后,在项目的根目录($GOPATH/src/test)中输入命令*"go mod init"和"go mod tidy"*,项目文件结构如下:
/src
/.vscode
-launch.json
/gitbub.com
/test
/main
-main.go (这里:import "test/xxx")
/xxx
-xxx.go (这里:package xxx)
-go.mod (几乎为空 - 第1行:module test
第2行:
第3行:go 1.16 )
我应该怎么做才能解决这个问题呢?
英文:
I'm new in GO and has got stuck in environment configuration for hours.
I succeeded to run some test projects, but when I tried to import my custom local packages (say xxx), the "go run" command failed, logging that:
*test/main/main.go:6:2: package test/xxx is not in GOROOT (/usr/local/go/src/test/xxx)*
It is strange that GOPATH seems to be ignored when importing local packages on my ubuntu computer.
go version is go1.16.5 linux/amd64
GOPATH is set using export GOPATH="$HOME/GoProjects"
GOROOT is set using export GOPATH="/usr/local/go"
GoMod is "on" using go env -w GO111MODULE=on
After editing project's .go files, command "go mod init" and "go mod tidy" are typed in the root folder of the project ($GOPATH/src/test), and the project file structure looks like:
/src
/.vscode
-launch.json
/gitbub.com
/test
/main
-main.go (here: import "test/xxx")
/xxx
-xxx.go (here: package xxx)
-go.mod (almost empty - line 1: module test
line 2:
line 3: go 1.16 )
What should I do to fix this problem?
答案1
得分: 4
假设(根据您的帖子不太清楚)整个目录树都在您的$HOME/GoProjects
下。在使用模块时,请不要将项目放在GOPATH中,因为混合使用GOPATH和模块只会导致问题。
首先,将$GOPATH
设置为类似$HOME/go
的路径,您可以在其中使用go get
获取您想要在项目中使用的各种工具(例如dlv
调试器等),但是__不要将您的项目放在其中__。
在启动新项目时,将其放在$GOPATH
之外的路径,例如$HOME/GoProjects/test
(您可以保留src
下的项目布局,但是不需要在那里创建src
目录):
GoProjects
/test
/main
-main.go
/xxx
-xxx.go
在项目目录(这里是test
)中运行go mod init NAME
,其中NAME是您的模块的名称。不要仅使用test
,而是使用完全限定的名称,该名称将将您的项目命名空间化到您的“域”中(例如github.com/yourusername/test
)。然后,在导入来自您的模块的包时将使用该名称。因此,在main.go
中,您将像这样导入xxx
包(需要有package xxx
):
import "github.com/yourusername/test/xxx"
在此处查看有关模块的更多信息:https://blog.golang.org/using-go-modules
英文:
Assuming (not clear from your post) this entire tree is under your $HOME/GoProjects
. When using modules; don't put your project inside GOPATH as mixing both GOPATH and modules only leads to problems.
First, set $GOPATH
to something like $HOME/go
which you can use to go get
various tools you want to use across projects (such as e.g. dlv
debugger and so on) but do not put your projects in there.
When starting a new project put it outside $GOPATH
to $HOME/GoProjects/test
(you can retain project layout you have under src
but you do not need src
there):
GoProjects
/test
/main
-main.go
/xxx
-xxx.go
In project directory (here test
) run go mod init NAME
where NAME is name of your module. Do not use just test
but fully qualified name which will namespace your project to your "domain" (e.g. github.com/yourusername/test
). That name will then be used when importing packages from your module. So in main.go
you will import xxx
package (needs to have package xxx
) like this:
import "github.com/yourusername/test/xxx"
See more info about modules here: https://blog.golang.org/using-go-modules
答案2
得分: 1
首先,不要再担心GOPATH
了。那是过时的东西,你不再需要使用它。然后,在某个地方创建一个名为test
的文件夹(与GOPATH
无关,只需放在当前目录或任何你想要的位置)。然后创建test/xxx
文件夹。接着创建test/xxx/xxx.go
文件:
package xxx
const X = 1
然后创建test/main
文件夹。接着创建test/main/main.go
文件:
package main
import "test/xxx"
func main() {
println(xxx.X)
}
然后回到test
目录,执行go mod init test
命令。然后回到test/main
目录,执行go build
命令。完成。
英文:
First, stop worrying about GOPATH
. That is old stuff, and you don't need to do it any more. Then, make a folder somewhere test
(doesn't need to be anything to do with GOPATH
, just put in current directory, or wherever you want). Then make test/xxx
. Then make test/xxx/xxx.go
:
package xxx
const X = 1
Then make folder test/main
. Then make test/main/main.go
:
package main
import "test/xxx"
func main() {
println(xxx.X)
}
Then go back to test
, and do go mod init test
. Then go back to test/main
,
and do go build
. Done.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论