如何在没有 gopath 的情况下导入本地包 [英] How to import local packages without gopath

查看:22
本文介绍了如何在没有 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.

推荐答案

Go依赖管理总结:

  • vgo 如果你的 go 版本是:x >= go 1.11
  • depvendor 如果你的 go 版本是:go 1.6 >= x <去 1.11
  • 如果你的 go 版本是手动的:x <去 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

编辑 3:Go 1.11 有一个功能 vgo替换 dep.

Edit 3: Go 1.11 has a feature vgo which will replace dep.

要使用 vgo,请参阅 Modules 文档.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.

编辑 2:销售方法仍然有效并且可以正常工作.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.

编辑 1:虽然我的旧方法有效,但它不再是正确"的方法.您应该使用 Go 1.6 中默认启用的 vendor 功能、vgodep(目前);.您基本上在 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 的子文件夹,然后在 binary1 中使用 import "./package1" 导入,我可以使用 GOPATH 导入本地包.gobinary2.go 脚本如下:

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天全站免登陆