The title pretty much sums it up. How can I determine what the values of GOOS and GOARCH was given just the go executable?
1条回答 默认 最新
- duanrong3308 2016-02-27 12:32关注
EDIT: The behavior of
runtime.GOROOT()
changed in Go 1.10, for details, see Go 1.10 release notes # Runtime. Basically nowruntime.GOROOT()
checks if theGOROOT
environment variable is set, and if so, its value is returned. If not, it returns theGOROOT
value recorded at compile time.
Check out the
runtime
package: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.
A list of possible combinations for
GOARCH
andGOOS
can be found here: https://golang.org/doc/install/source#environmentSo 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) }
Let's say
GOOS=windows
andGOARCH=amd64
. Running it withgo run xx.go
will print:windows amd64
Build an exe from it (e.g.
go build
). Running the exe has the same output.Now change
GOARCH
to386
. If you run it withgo run
(or build an exe and run that), it will print:windows 386
If you run the previously built exe, it will still print:
windows amd64
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报