问题描述
我已经使用过GOPATH
,但是对于当前的问题,我所遇到的问题没有帮助.我希望能够创建特定于项目的软件包:
I've used GOPATH
but for this current issue I'm facing it does not help. I want to be able to create packages that are specific to a project:
myproject/
├── binary1.go
├── binary2.go
├── package1.go
└── package2.go
我尝试了多种方法,但是如何使package1.go
在binary1.go
或binary2.go
中工作,依此类推?
I tried multiple ways but how do I get package1.go
to work in the binary1.go
or the binary2.go
and so on?
例如;我希望能够import "package1"
然后能够运行go build binary1.go
,并且一切正常,而不会引发在GOROOT
或GOPATH
上找不到该软件包的错误.我之所以需要这种功能,是因为是大型项目.我不想引用多个其他软件包或将它们保存在一个大文件中.
For example; I want to be able to import "package1"
and then be able to run go build binary1.go
and everything works fine without the error being thrown that the package cannot be found on GOROOT
or GOPATH
. The reason why I need this kind of functionality is for large scale projects; I do not want to have to reference multiple other packages or keep them in one large file.
推荐答案
转到依赖项管理摘要:
-
vgo
(如果您使用的版本是:x >= go 1.11
) -
dep
或vendor
(如果您使用的版本是:go 1.6 >= x < go 1.11
) - 如果您使用的版本是:
x < go 1.6
,请手动
vgo
if your go version is:x >= go 1.11
dep
orvendor
if your go version is:go 1.6 >= x < go 1.11
- Manually if your go version is:
x < go 1.6
Go 1.11具有功能vgo
,它将替换 dep
.
Edit 3: Go 1.11 has a feature vgo
which will replace dep
.
要使用vgo
,请参见模块文档.下面的TLDR:
To use vgo
, see Modules documentation. TLDR below:
export GO111MODULE=on
go mod init
go mod vendor # if you have vendor/ folder, will automatically integrate
go build
此方法在您的项目目录中创建一个名为go.mod
的文件.然后,您可以使用go build
构建项目.如果设置了GO111MODULE=auto
,则您的项目不能在$GOPATH
中.
This method creates a file called go.mod
in your projects directory. You can then build your project with go build
. If GO111MODULE=auto
is set, then your project cannot be in $GOPATH
.
供应商方法仍然有效,并且可以正常工作. vendor
在很大程度上是一个手动过程,因为创建了dep
和vgo
.
Edit 2: The vendoring method is still valid and works without issue. vendor
is largely a manual process, because of this dep
and vgo
were created.
虽然我的旧方法有效,但不再是正确"的方法.您应该使用供应商功能,vgo
或dep
(目前),这些功能在Go 1.6中已默认启用; 参见.您基本上是在vendor
目录中添加外部"或从属"程序包.编译后,编译器将首先使用这些软件包.
Edit 1: While my old way works it's not longer the "correct" way to do it. You should be using vendor capabilities, vgo
, or dep
(for now) that are enabled by default in Go 1.6; see. You basically add your "external" or "dependent" packages within a vendor
directory; upon compilation the compiler will use these packages first.
找到了.我可以通过创建package1
的子文件夹然后使用import "./package1"
在binary1.go
和binary2.go
脚本中导入package1
来导入GOPATH
本地包:
Found. I was able import local package with GOPATH
by creating a subfolder of package1
and then importing with import "./package1"
in binary1.go
and binary2.go
scripts like this :
binary1.go
binary1.go
...
import (
"./package1"
)
...
所以我当前的目录结构如下:
So my current directory structure looks like this:
myproject/
├── binary1.go
├── binary2.go
├── package1/
│ └── package1.go
└── package2.go
我还应该注意,相对路径(至少在go 1.5中)也有效;例如:
I should also note that relative paths (at least in go 1.5) also work; for example:
import "../packageX"
这篇关于如何在不使用gopath的情况下导入本地软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!