Home > Article > Backend Development > How to Retrieve GOOS and GOARCH Values from a Go Executable?
When given an executable file, ascertaining the values of GOOS (operating system) and GOARCH (processor architecture) used during its compilation becomes necessary. This inquiry focuses on identifying a mechanism for retrieving those values from the executable itself.
The runtime package in Go offers insights into the runtime characteristics of a program. Specifically, it contains constants or functions that provide information about the GOOS, GOARCH, GOPATH, and GOROOT environment variables.
To determine the GOOS and GOARCH values, the runtime package provides the following constants:
These constants hold the exact values that were set at compile time.
Consider the following code snippet:
package main import ( "fmt" "runtime" ) func main() { fmt.Println(runtime.GOOS) fmt.Println(runtime.GOARCH) }
When this program is executed, it will output the values of GOOS and GOARCH specified during compilation. For example, if GOOS is set to "windows" and GOARCH is set to "amd64," the program will print:
windows amd64
This will remain true even if GOOS and GOARCH are subsequently modified. The values stored in the runtime package constants remain those specified at compilation time.
The above is the detailed content of How to Retrieve GOOS and GOARCH Values from a Go Executable?. For more information, please follow other related articles on the PHP Chinese website!