标题几乎概括了它。我如何确定 go 可执行文件给出的 GOOS 和 GOARCH 的值是多少?
最佳答案
编辑 runtime.GOROOT()
的行为在 Go 1.10 中发生了变化,详情参见 Go 1.10 release notes # Runtime .基本上现在 runtime.GOROOT()
检查是否设置了 GOROOT
环境变量,如果是,则返回它的值。如果不是,则返回编译时记录的GOROOT
值。
查看 runtime
包裹:
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.
可在此处找到 GOARCH
和 GOOS
的可能组合列表:https://golang.org/doc/install/source#environment
所以你正在寻找的是 runtime
包中的常量:
runtime.GOOS
runtime.GOARCH
它们将准确包含构建应用时存在的值。
例如看这个简单的应用程序:
func main() {
fmt.Println(runtime.GOOS)
fmt.Println(runtime.GOARCH)
}
假设 GOOS=windows
和 GOARCH=amd64
。使用 go run xx.go
运行它会打印:
windows
amd64
从中构建一个 exe(例如 go build
)。运行 exe 具有相同的输出。
现在将 GOARCH
更改为 386
。如果您使用 go run
运行它(或构建一个 exe 并运行它),它将打印:
windows
386
如果你运行之前构建的exe,它仍然会打印:
windows
amd64
关于go - 给定一个可执行文件,我可以确定用于构建它的 GOOS 和 GOARCH 的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35669727/