本文介绍了给定可执行文件,我可以确定用于构建它的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中已更改,有关详细信息,请参见转到1.10发行说明#运行时.现在基本上runtime.GOROOT()检查是否设置了GOROOT环境变量,如果已设置,则返回其值.如果不是,则返回在编译时记录的GOROOT值.

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:

可以在此处找到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的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:55