Set the environment variable GOARCH to the value amd64. This instructs the go command to generate files for amd64. Other valid values for GOARCH include 386, arm, arm64, and others.
Set the environment variable GOARCH to the value amd64. This instructs the go command to generate files for amd64. Other valid values for GOARCH include 386, arm, arm64, and others.
F.Y.I.
The Go compilers support the following instruction sets:
- amd64, 386
- The x86 instruction set, 64- and 32-bit.
- arm64, arm
- The ARM instruction set, 64-bit (AArch64) and 32-bit.
- mips64, mips64le, mips, mipsle
- The MIPS instruction set, big- and little-endian, 64- and 32-bit.
- ppc64, ppc64le
- The 64-bit PowerPC instruction set, big- and little-endian.
- riscv64
- The 64-bit RISC-V instruction set.
- s390x
- The IBM z/Architecture.
- wasm
- WebAssembly.
(from: Introduction | Installing Go from source | Doc @ golang.org)
Also, you can go tool dist list to check the available architectures to build in your machine.
Copy$ go tool dist list
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
dragonfly/amd64
freebsd/386
(* snip *)
To build a static binary for macOS (Intel/ARM64) would be as below. In this manner, I suppose GOOS="darwin" GOARCH="arm64" combination will be for M1 architecture.
CopyMyVar="foo"
CGO_ENABLED=0 \
GOOS="darwin" \
GOARCH="amd64" \
GOARM="" \
go build \
-ldflags="-s -w -extldflags \"-static\" -X 'main.myVar=${MyVar}'" \
-o="/path/to/export/bin/myApp" \
"/path/to/main.go"
To compile for Linux on ARM v6, such as RaspberryPi Zero W, the combination would be as below.
Copy$ CGO_ENABLED=0 GOOS="linux" GOARCH="arm" GOARM="6" go build .
Note that those values are defined in:
src/internal/syslist/syslist.go, anddoc/install/source#environment.
With Go 1.5 (Q3 2015), GOARCH will become much more complete.
See commit 1eebb91 by Minux Ma (minux)
go/build: reserveGOARCHvalues for all common architecturesWhenever we introduce a new
GOARCH, older Go releases won't recognize them and this causes trouble for both our users and us (we need to add unnecessary build tags).Go 1.5 has introduced three new GOARCHes so far:
arm64 ppc64 ppc64le, we can take the time to introduce GOARCHes for all common architectures that Go might support in the future to avoid the problem.
Copyconst goosList = "android darwin dragonfly freebsd linux nacl \
netbsd openbsd plan9 solaris windows "
const goarchList = "386 amd64 amd64p32 arm arm64 ppc64 ppc64le \
mips mipsle mips64 mips64le mips64p32 mips64p32le \ # (new)
ppc s390 s390x sparc sparc64 " # (new)
The list is still being review in Change 9644, with comments like:
I wouldn't bother with Itanium. It's basically a dead architecture.
Plus, it's so hard to write a compiler for it that I really can't see it happening except as a labor of love, and nobody loves the Itanium.
The official documentation now (GO 1.5+ Q3 2015) reflects that completed list.
Update 2018: as documented in Giorgos Oikonomou's answer, Go 1.7 (Q1 2016) has introduced the
go tool dist list command.
See commit c3ecded: it fixes issue 12270 opened in Q3 2015:
To easier write tooling for cross compiling it would be good to programmatically get the possible combinations of GOOS and GOARCH.
This was implemented in CL 19837
cmd/dist: introducelistsubcommand to list all supported platforms
You can list in plain text, or in json:
Copy> go tool dist list -json
[
{
"GOOS": "android",
"GOARCH": "386",
"CgoSupported": true
},
...
]
As Mark Bates tweeted:
Bonus: Column output properly formatted for display:
Copygo tool dist list | column -c 75 | column -t
I think you're looking for this list of possible GOOS and GOARCH combinations, in this section:
http://golang.org/doc/install/source#environment
$GOOS and $GOARCH The name of the target operating system and compilation architecture. These default to the values of $GOHOSTOS and $GOHOSTARCH respectively (described below).
Choices for $GOOS are darwin (Mac OS X 10.8 and above and iOS), dragonfly, freebsd, linux, netbsd, openbsd, plan9, solaris and windows. Choices for $GOARCH are amd64 (64-bit x86, the most mature port), 386 (32-bit x86), arm (32-bit ARM), arm64 (64-bit ARM), ppc64le (PowerPC 64-bit, little-endian), ppc64 (PowerPC 64-bit, big-endian), mips64le (MIPS 64-bit, little-endian), and mips64 (MIPS 64-bit, big-endian). mipsle (MIPS 32-bit, little-endian), and mips (MIPS 32-bit, big-endian).
The valid combinations of $GOOS and $GOARCH are:
Copy$GOOS $GOARCH android arm darwin 386 darwin amd64 darwin arm darwin arm64 dragonfly amd64 freebsd 386 freebsd amd64 freebsd arm linux 386 linux amd64 linux arm linux arm64 linux ppc64 linux ppc64le linux mips linux mipsle linux mips64 linux mips64le netbsd 386 netbsd amd64 netbsd arm openbsd 386 openbsd amd64 openbsd arm plan9 386 plan9 amd64 solaris amd64 windows 386 windows amd64