Home > Article > Backend Development > How to Identify the GOOS and GOARCH Values of an Executable Without Its Codebase?
Determining GOOS and GOARCH Values of an Executable
It is possible to ascertain the values of GOOS and GOARCH, the operating system and processor architecture respectively, used to build an executable without possessing the codebase itself. This information is available through constants in the runtime package.
runtime.GOOS and runtime.GOARCH
The runtime package provides two constants, runtime.GOOS and runtime.GOARCH, which reveal the values of the corresponding environment variables at the time of compilation. These constants are accessible throughout the executable's execution, despite not influencing its runtime behavior.
Example Implementation
The following simple Go program illustrates how to retrieve the runtime OS and architecture information:
<code class="go">package main import ( "fmt" "runtime" ) func main() { fmt.Println("OS:", runtime.GOOS) fmt.Println("Architecture:", runtime.GOARCH) }</code>
Compiling and running this program with specified GOOS and GOARCH values (e.g., GOOS=windows GOARCH=amd64) prints the expected output:
OS: windows Architecture: amd64
Note
Prior to Go 1.10, runtime.GOROOT() would return the GOROOT value recorded at compile time. However, in Go 1.10 and later, it checks for the GOROOT environment variable and uses its value if set.
The above is the detailed content of How to Identify the GOOS and GOARCH Values of an Executable Without Its Codebase?. For more information, please follow other related articles on the PHP Chinese website!