4

I have situation with a project.

It behave different when i use go module, outside GOPATH, and "go get" inside GOPATH. In both cases build goes without errors.

But GPRC connection behaves differently. Gives timeout in "go mod" case, works fine with "go get".

I suspect that go uses different set of packages. I need full list of used packages with versions in both modes to compare. How can i access it?

1 Answers1

3

For listing installed packages using GOPATH, please see this old thread: How to list installed go packages

The following applies to the new module mode.

At compile / build time

You may use the go list -m all command to view final versions that will be used in a build for all direct and indirect dependencies (source). You can read more details about this here: Modules: Version Selection.

At runtime

At runtime (from your application) you may use the debug.ReadBuildInfo() function:

ReadBuildInfo returns the build information embedded in the running binary. The information is available only in binaries built with module support.

Note: debug.ReadBuildInfo() was only added in Go 1.12 (released just a day ago).

Example getting and printing build info (recursively). Easiest is to JSON-marshal the build info:

bi, ok := debug.ReadBuildInfo()
if !ok {
    fmt.Println("Getting build info failed (not in module mode?)!")
    return
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "  ")
if err := enc.Encode(bi); err != nil {
    panic(err)
}

Example output

Example output for a project which has a single dependency: github.com/globalsign/mgo).

Running go list -m all:

mytest
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8

Getting and JSON-marshaling the build info at runtime:

{
  "Path": "mytest",
  "Main": {
    "Path": "mytest",
    "Version": "(devel)",
    "Sum": "",
    "Replace": null
  },
  "Deps": [
    {
      "Path": "github.com/globalsign/mgo",
      "Version": "v0.0.0-20181015135952-eeefdecb41b8",
      "Sum": "h1:DujepqpGd1hyOd7aW59XpK7Qymp8iy83xq74fLr21is=",
      "Replace": null
    }
  ]
}
icza
  • 389,944
  • 63
  • 907
  • 827
  • I need something that i can compare. Neither "go list -f "{{.ImportPath}} {{.Deps}}" ./..." or "go list ./..." doesn't give me package versions. "go list -m all" do the job for module version. – Alexander Kochetkov Feb 27 '19 at 06:56
  • @AlexanderKochetkov There is no recorded package version in `GOPATH` mode, `go get` always gets the latest commit on `master` branch. – icza Feb 27 '19 at 07:05
  • @AlexanderKochetkov So think of this way: if packages in your `GOPATH` are up-to-date, and if your module uses a different version than the latest of master, there is always a chance they will behave differently. – icza Feb 27 '19 at 07:09
  • Finally i solve my problem, by forcing all go.mod versions to master. For example "google.golang.org/grpc master". So, by default go fills it with something else i guess. – Alexander Kochetkov Feb 27 '19 at 12:42
  • @AlexanderKochetkov In module mode, go does not choose the master, the version selection strategy is linked in the answer and is described here: [Module version selection](https://github.com/golang/go/wiki/Modules#version-selection). In short, the highest version is chosen that suits all modules. – icza Feb 27 '19 at 13:03