给定可执行文件,我可以确定用于构建它的GOOS和GOARCH的值吗? [英] Given an executable can I determine values of GOOS and GOARCH used to build it?

查看:170
本文介绍了给定可执行文件,我可以确定用于构建它的GOOS和GOARCH的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎总结了一下.如何确定仅go可执行文件才获得了GOOS和GOARCH的值?

The title pretty much sums it up. How can I determine what the values of GOOS and GOARCH was given just the go executable?

推荐答案

编辑:runtime.GOROOT()的行为在Go 1.10中已更改,有关详细信息,请参见

The behavior of runtime.GOROOT() changed in Go 1.10, for details, see Go 1.10 release notes # Runtime. Basically now runtime.GOROOT() checks if the GOROOT environment variable is set, and if so, its value is returned. If not, it returns the GOROOT value recorded at compile time.

检出 runtime 软件包:

Check out the runtime package:

GOARCH,GOOS,GOPATH和GOROOT环境变量完善了Go环境变量集.它们会影响Go程序的构建(请参见 https://golang.org/cmd/go https://golang.org/pkg/go/build ). GOARCH,GOOS和GOROOT会在编译时记录,并且可以通过此包中的常量或函数使用,但它们不会影响运行时系统的执行.

The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete the set of Go environment variables. They influence the building of Go programs (see https://golang.org/cmd/go and https://golang.org/pkg/go/build). GOARCH, GOOS, and GOROOT are recorded at compile time and made available by constants or functions in this package, but they do not influence the execution of the run-time system.

可以在此处找到GOARCHGOOS的可能组合的列表: https ://golang.org/doc/install/source#environment

A list of possible combinations for GOARCH and GOOS can be found here: https://golang.org/doc/install/source#environment

所以您要查找的是runtime包中的常量:

So what you are looking for are constants in the runtime package:

runtime.GOOS
runtime.GOARCH

它们将完全包含构建您的应用程序时出现的值.

And they will exactly contain the values that were present when your app was built.

例如,请参见以下简单应用程序:

For example see this simple app:

func main() {
    fmt.Println(runtime.GOOS)
    fmt.Println(runtime.GOARCH)
}

让我们说GOOS=windowsGOARCH=amd64.用go run xx.go运行它将打印:

Let's say GOOS=windows and GOARCH=amd64. Running it with go run xx.go will print:

windows
amd64

从中构建一个exe(例如go build).运行exe具有相同的输出.

Build an exe from it (e.g. go build). Running the exe has the same output.

现在将GOARCH更改为386.如果使用go run运行它(或构建一个exe并运行该文件),它将显示:

Now change GOARCH to 386. If you run it with go run (or build an exe and run that), it will print:

windows
386

如果运行以前构建的exe,它将仍然打印:

If you run the previously built exe, it will still print:

windows
amd64

这篇关于给定可执行文件,我可以确定用于构建它的GOOS和GOARCH的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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