doudu8291 2019-02-26 16:36
浏览 454
已采纳

如何使用GoPATH和“ go module”模式下的所有使用过的软件包获取详细的构建日志?

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条回答 默认 最新

  • douweicheng5532 2019-02-26 16:52
    关注

    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
        }
      ]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,