Golang编译同一个包来自两个不同的文件位置

huangapple go评论91阅读模式
英文:

golang compiling the same package from two different file locations

问题

我将为您翻译以下内容:

我将我的项目结构组织如下:

/workspace
  /src
    /package1
  /vendor
    /src
      /somepackage
      /anotherpackage
      /package1

我的GOPATH设置为 /workspace;/workspace/vendor

请注意,这里没有使用go 1.5的vendor选项。

到目前为止,在我们的构建/开发工作流中,一切都能正常编译和工作。现在我遇到了这样的情况:我想将一个库导入到vendor目录workspace/vendor/src/package1中,但是在workspace/src/package1目录中编写一些单元测试。

当运行测试时,它无法找到vendor目录中package1的方法。

有没有办法让vendor包的代码被识别为同一个命名空间中的代码?

英文:

The way I have my projects structured is similar to the following

/workspace
  /src
    /package1
  /vendor
    /src
      /somepackage
      /anotherpackage
      /package1

My GOPATH is set to /workspace;/workspace/vendor

Note this is not using the go 1.5 vendor option.

So far everything has been compiling and working fine within out build / development workflow.
I'm in a situation now where I would like to import a library into the vendor directory workspace/vendor/src/package1 but write some unit tests in the workspace/src/package1 directory..

When the tests run it cannot find methods from the package1 in the vendor dir.

Is there a way to get the vendor package code recognised into the same namespace like this?

答案1

得分: 2

你是否要求将一个包的代码“分割”到两个不同的gopath文件夹中?go工具无法做到这一点,因为它会在gopath中的任何文件夹中找到的第一个文件夹。如果你正在积极地开发一个项目,为什么要将它放在vendor的gopath而不是src的gopath中呢?

正是因为这样的区别,我通常建议将所有内容都放在一个gopath中。如果你想要对依赖进行打包,我建议为每个主要的包都这样做。

英文:

Are you asking to essentially "split" the code for a package between two folders in two different gopaths? The go tool cannot do this, as it takes the first folder it finds on any folder in your gopath. If you are actively working on a project, why would it go in the vendor gopath and not in the src one?

It is because of distinctions like this that I generally recommend one gopath for everything. If you want to vendor dependencies I recommend doing that for each individual main package you have.

答案2

得分: 1

根据captncraig的说法:go工具无法做到这一点。

但你可以自由地调用go编译器来处理任意一组文件:go tool compile <file.go>...

当然,这将重新引入一种类似于Makefile的构建系统。这是可行的,但是由go buildgo install完成的大部分工作将丧失,需要在你的Makefile中重新实现。

英文:

As captncraig said: The go tool cannot do this.

But you are free to call the go compiler itself on any set of files you want: go tool compile &lt;file.go&gt;...

Of course this would reintroduce some kind of Makefile style build system. It is doable but all the heavy lifting done by go build or go install is lost and will have to live in your Makefiles.

huangapple
  • 本文由 发表于 2015年10月27日 07:22:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/33357309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定