寻找Go程序的导入和依赖项。

huangapple go评论79阅读模式
英文:

Finding imports and dependencies of a go program

问题

go list -json命令在命令行中运行可以告诉你一个Go程序的导入和依赖(以JSON格式)。有没有办法在Go程序内部获取这些信息,即在运行时通过某种方式运行go list命令或其他方式获取?

英文:

The go list -json command run from the command line will tell you the imports and dependencies of a go program ( in json format). Is there a way to get this information from within a go program I.e at runtime, either by running the 'go list' command somehow or another way?

答案1

得分: 4

以下代码使用go/build来获取当前工作目录中应用程序的导入项。

p, err := build.Default.Import(".", ".", 0)
if err != nil {
   // 处理错误
}
for _, i := range p.Imports {
   fmt.Println(i)
}

您可以使用一个简单的递归函数构建所有依赖项的列表。

要获取特定path的导入项,请使用:

p, err := build.Default.Import(path, ".", 0)
if err != nil {
   // 处理错误
}
for _, i := range p.Imports {
   fmt.Println(i)
}
英文:

The following code uses the go/build to get the imports for the application in the current working directory.

p, err := build.Default.Import(".", ".", 0)
if err != nil {
   // handle error
}
for _, i := range p.Imports {
   fmt.Println(i)
}

You can build a list of all dependencies using a simple recursive function.

To get the imports for a specific path, use:

p, err := build.Default.Import(path, ".", 0)
if err != nil {
   // handle error
}
for _, i := range p.Imports {
   fmt.Println(i)
}

答案2

得分: 3

我不认为你可以在不使用go二进制文件的情况下完成这个任务,因为go需要分析你的源代码。

这个任务相当简单,但是它必须在运行时访问go和你的源代码。以下是一个快速示例:

package main

import (
	"encoding/json"
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("go", "list", "-json")
	stdout, err := cmd.Output()
	if err != nil {
		println(err.Error())
		return
	}

	var list GoList
	err = json.Unmarshal(stdout, &list)

	for _, d := range list.Deps {
		fmt.Printf(" - %s\n", d)
	}
}

type GoList struct {
	Dir        string
	ImportPath string
	Name       string
	Target     string
	Stale      bool
	Root       string
	GoFiles    []string
	Imports    []string
	Deps       []string
}

希望对你有帮助!

英文:

I don't think you can do it without using the go binary since go needs to analyze your source code.

It's pretty easy to do but it must have access to go and your source code at run time. Heres a quick example:

package main

import (
	"encoding/json"
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("go", "list", "-json")
	stdout, err := cmd.Output()
	if err != nil {
		println(err.Error())
		return
	}

	var list GoList
	err = json.Unmarshal(stdout, &list)

	for _, d := range list.Deps {
		fmt.Printf(" - %s\n", d)
	}
}

type GoList struct {
	Dir        string
	ImportPath string
	Name       string
	Target     string
	Stale      bool
	Root       string
	GoFiles    []string
	Imports    []string
	Deps       []string
}

答案3

得分: 1

不,我认为在没有可靠的源代码的情况下,这是不可能的。在不同平台上使用不同编译器编译的Go二进制文件可能有可能没有这些信息编译进去(或者将来可能没有)。但是既然Go程序已经被编译了,为什么不在你还可以访问源代码的时候记录这些信息呢?

英文:

No, I don't think this is possible without the source in a reliable way. Go binaries on different platforms, compiled with different compilers may or may not have (or may not have in the future) these informations compiled in.

But as Go programs are compiled anyway: Why not record this information while you do have access to the source code?

huangapple
  • 本文由 发表于 2015年3月17日 05:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/29087241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定