Go:非本地包中的本地导入 [英] Go: local import in non-local package
问题描述
我有以下文件结构:
<预><代码>.├── 仓│ └── 你好├── pkg└── src└── jacob.uk.com├── 问候│ └── greeting.go└── helloworld.go5个目录,3个文件使用以下GOPATH
/Users/clarkj84/Desktop/LearningGo
在 src
文件夹中执行 /usr/local/go/bin/go install jacob.uk.com
后,我收到错误 local import "./greeting" 在非本地包中
helloworld.go
:
package main;导入./问候"功能主(){}
当指定一个非本地包到 go install
时,你不能使用本地导入.如果您希望本地导入工作,首先将工作目录更改为 src/jacob.uk.com
然后执行 go install
(不指定包).
当然,如果你提供了 helloworld.go
,你会得到一个编译错误:imported and not used
.但是一旦你使用了导入的 greeting
包中的东西,它应该可以编译.
但是您根本不应该使用本地导入.而是写:
导入jacob.uk.com/greeting"
这样做你就可以从任何地方编译/运行/安装它.
I have the following file structure:
.
├── bin
│ └── hello
├── pkg
└── src
└── jacob.uk.com
├── greeting
│ └── greeting.go
└── helloworld.go
5 directories, 3 files
With the following GOPATH
/Users/clarkj84/Desktop/LearningGo
Upon executing /usr/local/go/bin/go install jacob.uk.com
within the src
folder, I get the error local import "./greeting" in non-local package
helloworld.go
:
package main;
import "./greeting"
func main() {
}
You can't use local import when specifying a non-local package to go install
. If you want the local import to work, first change working directory to src/jacob.uk.com
then execute go install
(without specifying the package).
Of course having the helloworld.go
you provided you will get an compile error: imported and not used
. But once you use something from the imported greeting
package, it should compile.
But you shouldn't use local imports at all. Instead write:
import "jacob.uk.com/greeting"
And doing so you will be able to compile/run/install it from anywhere.
这篇关于Go:非本地包中的本地导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!