List embedded dependencies in a Go binary

911 views
Skip to first unread message

Michel Casabianca

unread,
Nov 2, 2021, 1:18:15 PM11/2/21
to golang-nuts
Hi gophers,

I would like to know which dependencies are embedded in generated Go binary, along with their version. I though at first that they were listed in go.sum file, but it seems that this is not the case.

I found two different ways to do so:
- Running "go version -m my-binary"
- Calling runtime.debug API in my code, as follows:

func buildInfo() {
    buildInfo, ok := debug.ReadBuildInfo()
    if !ok {
        panic("Can't read BuildInfo")
    }
    fmt.Println("Dependencies:")
    for _, dep := range buildInfo.Deps {
        fmt.Printf(" %s %s\n", dep.Path, dep.Version)
    }
}

func main() {
    // print build info if -info passed on command line
    if len(os.Args) > 1 && os.Args[1] == "-info" {
        buildInfo()
        os.Exit(0)
    }
    ...
}

These two methods produce the same list. Does anyone confirm that this is the way to list embedded dependencies in a Go binary?

Another question related to this one:

Let's say I use dependencies A and B that both use C in two different versions. According to my own experience, it seems that Go will embed only one of these versions, the latest one. Is this correct?

Best regards

jfcg...@gmail.com

unread,
Nov 3, 2021, 7:29:27 AM11/3/21
to golang-nuts
try "go version -m"
Reply all
Reply to author
Forward
0 new messages