如何在不使用gopath的情况下导入本地软件包 [英] How to import local packages without gopath

查看:201
本文介绍了如何在不使用gopath的情况下导入本地软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用过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.gobinary1.gobinary2.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,并且一切正常,而不会引发在GOROOTGOPATH上找不到该软件包的错误.我之所以需要这种功能,是因为是大型项目.我不想引用多个其他软件包或将它们保存在一个大文件中.

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
  • )
  • depvendor(如果您使用的版本是:go 1.6 >= x < go 1.11
  • )
  • 如果您使用的版本是:x < go 1.6
  • ,请手动
  • vgo if your go version is: x >= go 1.11
  • dep or vendor 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在很大程度上是一个手动过程,因为创建了depvgo.

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.

虽然我的旧方法有效,但不再是正确"的方法.您应该使用供应商功能,vgodep(目前),这些功能在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.gobinary2.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的情况下导入本地软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆