简体   繁体   中英

Fetching Go package dependencies using tags

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
(ie 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`

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM