如何获取Go详细的构建日志,以及所有在GOPATH和“ go module”中使用过的软件包模式? [英] How to get Go detailed build logs, with all used packages in GOPATH and "go module" mode?

查看:215
本文介绍了如何获取Go详细的构建日志,以及所有在GOPATH和“ go module”中使用过的软件包模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目的情况。

当我在GOPATH外部使用go模块和在GOPATH内部使用 go get方法时,其行为会有所不同。在两种情况下,构建都不会出错。

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

但是GPRC连接的行为有所不同。在 go mod情况下给出超时,在 go get下可以正常工作。

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

我怀疑go使用了不同的软件包。我需要两种模式下使用过的软件包的完整列表进行比较。我如何访问它?

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?

推荐答案

使用 GOPATH 列出已安装的软件包,请参见以下旧主题:如何列出已安装的go软件包

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.

您可以使用 go list -m all 命令查看将在构建中用于所有直接和间接依赖项的最终版本()。您可以在此处阅读有关此内容的更多详细信息:模块:版本选择

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.

在运行时(来自您的应用程序),您可以使用 debug.ReadBuildInfo() 函数:

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


ReadBuildInfo返回嵌入在正在运行的二进制文件中的构建信息。该信息仅在模块支持的二进制文件中可用。

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

注意: debug.ReadBuildInfo()仅添加到转到1.12 (仅发布一天

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

示例获取和打印构建信息(递归)。最简单的方法是对构建信息进行JSON编组:

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)
}



示例输出



具有单个依赖项的项目的示例输出: github.com/globalsign/mgo )。

运行 go list -m all

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

在运行时获取构建信息并对其进行JSON封送处理:

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
    }
  ]
}

这篇关于如何获取Go详细的构建日志,以及所有在GOPATH和“ go module”中使用过的软件包模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆