Home > Article > Backend Development > How Do You Retrieve GOOS and GOARCH Values from a Compiled Go Executable?
Determining GOOS and GOARCH Values for Built Go Executables
In Go, the GOOS and GOARCH environment variables define the target operating system and architecture for which a program is built. This information is crucial for compiling and executing Go executables.
To retrieve these values for a compiled Go executable, you can utilize the runtime package. As stated in the documentation:
GOARCH, GOOS, and GOROOT are recorded at compile time
The runtime.GOOS and runtime.GOARCH constants provide access to these values. They reflect the exact settings used during the compilation of your executable.
To illustrate this, consider the following simple Go program:
<code class="go">package main import "fmt" func main() { fmt.Println(runtime.GOOS) fmt.Println(runtime.GOARCH) }</code>
When you build this program into an executable, the recorded values of GOOS and GOARCH will persist within the executable. Running the executable will output the target operating system and architecture used during its compilation.
For example, if the executable was built with GOOS=linux and GOARCH=amd64, executing it would print the following:
linux amd64
Even if you subsequently modify the environment variables, the executable will still output the originally recorded values.
Therefore, by inspecting the runtime.GOOS and runtime.GOARCH constants, you can determine the target operating system and architecture for which a given Go executable was compiled.
The above is the detailed content of How Do You Retrieve GOOS and GOARCH Values from a Compiled Go Executable?. For more information, please follow other related articles on the PHP Chinese website!