Golang如何导入没有gopath的本地软件包? [英] Golang how to import local packages without gopath?

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

问题描述

我已经使用了 GOPATH ,但对于这个目前我遇到的问题并没有帮助。我希望能够创建特定于项目的包:

  myproject / 
├──binary1。 go
├──binary2.go
├──package1.go
└──package2.go

我尝试了多种方式,但是如何在 binary1.go中使用 package1.go code>或 binary2.go 等等?



例如;我希望能够导入package1,然后能够运行 go build binary1.go ,一切正常在没有错误的情况下抛出,在 GOROOT GOPATH 上找不到该错误。我需要这种功能的原因是大型项目;我不想引用多个其他包或将它们保存在一个大文件中。

编辑2:仍然有效,没有问题。 vendor 是事实上的管理项目的方式。然而,这主要是一个手动过程,因为这个新的工具已经出来为你管理你的供应商包: dep



dep 将成为未来的工具链;这不是我推荐的第三方工具。这是未来:)。这是一个非常简单的工具,请参阅文档



< hr>

编辑1:尽管我的旧方法有效,但它不再是正确的方式。您应该使用Go 1.6中默认启用的供应商功能; 请参阅。您基本上在供应商目录中添加您的外部或依赖包;编译后编译器会先使用这些包。






找到。通过创建 package1 的子文件夹,然后使用导入,我可以用 GOPATH 导入本地包。在 binary1.go binary2.go 这样的脚本中导入.package1

binary1.go

  ... 
导入(
./package1

...



<所以我目前的目录结构如下所示:

  myproject / 
├──binary1.go
├──binary2.go
├──package1 /
│└──package1.go
└──package2.go

我也应该注意到相对路径(至少在1.5版中)也可以使用;例如:

  import../packageX


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

I tried multiple ways but how do I get package1.go to work in the binary1.go or the binary2.go and so on?

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.

解决方案

Edit 2: The vendoring method is still valid and works without issue. vendor is the defacto way to manage projects. However, it's largely a manual process, because of this a new tool has came out to manage your vendor packages for you: dep

dep will be part of the toolchain in the future; this is not some third-party tool that I'm recommending. It's the future :). It's a very simple tool to use; see the documentation.


Edit 1: While my old way works it's not longer the "correct" way to do it. You should be using vendor capabilities 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.


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

...
import (
        "./package1"
      )
...

So my current directory structure looks like this:

myproject/
├── binary1.go
├── binary2.go
├── package1/
│   └── package1.go
└── package2.go

I should also note that relative paths (at least in go 1.5) also work; for example:

import "../packageX"

这篇关于Golang如何导入没有gopath的本地软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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