使用标签获取Go包的依赖项

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

Fetching Go package dependencies using tags

问题

如何使用特定导入的版本标签一次性获取Go包的所有依赖项?

假设我有一个包含多个包的Go树:

src/
    foo/
        bar/               (包bar中的Go代码)
            x.go
        quux/              (包main中的Go代码)
            y.go

现在假设包`bar'依赖于一个使用标签版本的第三方库
(通常使用以下命令获取:go get -tags mylib_2.0 github.com/user/mylib

我想要做的是在导入行上指定一个标签,
这样在我的树上运行**go get ./...**时可以获取正确的版本方案。类似于:

import "github.com/user/mylib" `tags=mylib_2.0`
英文:

How can I fetch all the dependencies of Go packages at once using version tags on specific imports ?

Say I have my go tree with multiple packages in it :

src/
    foo/
        bar/               (go code in package bar)
            x.go
        quux/              (go code in package main)
            y.go

Now let the package `bar' depends on a third party library that use tag versions
(i.e. usually fetched with : go get -tags mylib_2.0 github.com/user/mylib)

What I want to do is to specify a tag on the import line
so that go get ./... on my tree gets the correct version scheme. Something like:

import "github.com/user/mylib" `tags=mylib_2.0`

答案1

得分: 1

更改你的项目结构:

src/
    foo/
        bar/
           v1/    (bar包中的Go代码)
              x.go
           v2/
              x.go
        quux/ 
             v1/   (main包中的Go代码)
                y.go

这是处理不同版本库的唯一可能的方式。

通过这种方式,你也解决了jnml所描述的问题,现在每个库都有自己的依赖和其他库的版本。

根据评论更新:

根据这里描述的工作区文档:
http://golang.org/doc/code.html#Workspaces

你的结构将如下所示:

src/
    foo/
        bar/
           v1/    (bar包中的Go代码)
              x.go
           v2/
              x.go
        quux/ 
             v1/   (main包中的Go代码)
                y.go
        meier/ 
             v1/   (main包中的Go代码)
                w.go
             v2/   (main包中的Go代码)
                w.go

现在在你的*bar库(x.go)中,你需要来自quux库(y.go)*的函数
要导入它,你将写:

import "foo/quux/v1/"

只是作为注意,你也可以在结构中在包之前加上版本,所以你的结构可以看起来像/foo/v1/quux,那么你就不需要命名导入。

现在库quux使用版本1的库meier
所以导入将是:

import "foo/meier/v1"

关于jnml的问题,
现在你的库bar也需要版本2的库meier
所以导入将如下所示:

import "foo/meier/v2"

现在当你调用:bar -> quux -> meier
你会发现你不能将bar的返回值分配/传递给meier中的某个东西

因为:meier/v1 != meier/v2

这将在编译过程中失败。

如果你需要使用来自meier v1quux的结果,你还需要在bar中导入meier/v1

英文:

change you project structure too:

src/
    foo/
        bar/
           v1/    (go code in package bar)
              x.go
           v2/
              x.go
        quux/ 
             v1/   (go code in package main)
                y.go

this is the only possible way to handle different version of your libs.

with this you also solve the problem jnml described, now each lib would have its own dependencies and versions to other libs.

update due to comments:

according to workspace documentation described here:
http://golang.org/doc/code.html#Workspaces

your structure will looke like

src/
    foo/
        bar/
           v1/    (go code in package bar)
              x.go
           v2/
              x.go
        quux/ 
             v1/   (go code in package main)
                y.go
        meier/ 
             v1/   (go code in package main)
                w.go
             v2/   (go code in package main)
                w.go

now in your bar lib (x.go) you need functions from lib *quux (y.go)
to import this you will write :

import "foo/quux/v1/"

just as note you can also do the version before package in your structure so instead of foo/quux/v1 your structure could look like /foo/v1/quux, then you dont need to name the imports.

now lib quux uses lib meier in version 1
so import will be:

import "foo/meier/v1"

and regarding to jnml,
now you lib bar also needs lib meier but in version 2
so import will look like:

import "foo/meier/v2"

now when you call: bar -> quux -> meier
you will see that you can't assign /pass the return value in bar to something from meier

because: meier/v1 != meier/v2

and this will fail already during compilation.

if you need to work with result from quux which is coming from meier v1 you need to also import meier/v1 in bar

huangapple
  • 本文由 发表于 2013年6月24日 20:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/17275056.html
匿名

发表评论

匿名网友

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

确定