$GOARCH 目标平台(编译后的目标平台)的处理器架构(386、amd64、arm)
$GOOS 目标平台(编译后的目标平台)的操作系统(darwin、freebsd、linux、windows)
各平台的GOOS和GOARCH参考
OS ARCH OS version
linux 386 / amd64 / arm >= Linux 2.6
darwin 386 / amd64 OS X (Snow Leopard + Lion)
freebsd 386 / amd64 >= FreeBSD 7
windows 386 / amd64 >= Windows 2000
1、当前系统:
fuyundeMacBook-Pro:src fuyun$ uname -a
Darwin fuyundeMacBook-Pro.local 13.2.0 Darwin Kernel Version 13.2.0: Thu Apr 17 23:03:13 PDT 2014; root:xnu-2422.100.13~1/RELEASE_X86_64 x86_64
执行下面命令:
$ cd /usr/local/go/src
$ sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
这里 额外多一个环境变量 CGO_ENABLED 是因为 交叉编译不支持 CGO,我们这里禁用它。
这里并不是重新编译Go,因为安装Go的时候,只是编译了本地系统需要的东西;而需要跨平台交叉编译,需要在Go中增加对其他平台的支持。所以,有 ./make.bash 这么一个过程。
执行结果类似如下:
fuyundeMacBook-Pro:pkg fuyun$ cd /usr/local/go/src
fuyundeMacBook-Pro:src fuyun$ sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
Password:
# Building C bootstrap tool.
cmd/dist
# Building compilers and Go bootstrap tool for host, darwin/amd64.
lib9
libbio
liblink
misc/pprof
cmd/cc
cmd/gc
cmd/6l
cmd/6a
cmd/6c
cmd/6g
pkg/runtime
pkg/errors
pkg/sync/atomic
pkg/sync
pkg/io
pkg/unicode
pkg/unicode/utf8
pkg/unicode/utf16
pkg/bytes
pkg/math
pkg/strings
pkg/strconv
pkg/bufio
pkg/sort
pkg/container/heap
pkg/encoding/base64
pkg/syscall
pkg/time
pkg/os
pkg/reflect
pkg/fmt
pkg/encoding
pkg/encoding/json
pkg/flag
pkg/path/filepath
pkg/path
pkg/io/ioutil
pkg/log
pkg/regexp/syntax
pkg/regexp
pkg/go/token
pkg/go/scanner
pkg/go/ast
pkg/go/parser
pkg/os/exec
pkg/os/signal
pkg/net/url
pkg/text/template/parse
pkg/text/template
pkg/go/doc
pkg/go/build
cmd/go
pkg/runtime (linux/amd64)
# Building packages and commands for host, darwin/amd64.
runtime
errors
sync/atomic
unicode
unicode/utf8
math
sync
sort
container/heap
io
syscall
hash
encoding
unicode/utf16
hash/crc32
container/list
crypto/subtle
bytes
strings
bufio
text/tabwriter
path
crypto/cipher
time
crypto/hmac
compress/bzip2
hash/adler32
container/ring
hash/crc64
hash/fnv
html
image/color
image/color/palette
strconv
math/rand
math/cmplx
os
runtime/race
reflect
crypto
encoding/base64
crypto/md5
regexp/syntax
net/url
crypto/aes
path/filepath
crypto/rc4
io/ioutil
os/exec
crypto/sha1
regexp
crypto/sha256
crypto/sha512
encoding/pem
net
os/signal
encoding/ascii85
encoding/binary
fmt
encoding/base32
crypto/des
image
index/suffixarray
image/draw
image/jpeg
debug/dwarf
debug/gosym
debug/plan9obj
flag
log
debug/elf
debug/macho
debug/pe
go/token
encoding/json
go/scanner
go/ast
cmd/addr2line
encoding/xml
text/template/parse
go/parser
go/printer
text/template
compress/flate
math/big
compress/gzip
go/doc
encoding/hex
mime
cmd/cgo
go/format
cmd/fix
crypto/elliptic
go/build
crypto/ecdsa
crypto/rand
crypto/rsa
crypto/dsa
encoding/asn1
net/textproto
mime/multipart
crypto/x509/pkix
crypto/x509
runtime/pprof
cmd/gofmt
cmd/nm
crypto/tls
cmd/objdump
cmd/pack
cmd/yacc
net/http
archive/tar
archive/zip
compress/lzw
compress/zlib
database/sql/driver
encoding/csv
database/sql
encoding/gob
html/template
cmd/go
expvar
image/gif
image/png
log/syslog
net/http/cgi
net/http/cookiejar
net/http/httptest
net/http/httputil
net/http/fcgi
net/http/pprof
net/mail
net/rpc
net/smtp
os/user
runtime/debug
testing
testing/iotest
net/rpc/jsonrpc
testing/quick
text/scanner
# Building packages and commands for linux/amd64.
runtime
errors
sync/atomic
unicode
unicode/utf8
math
sync
sort
io
syscall
hash
container/heap
encoding
unicode/utf16
bytes
strings
bufio
text/tabwriter
path
hash/crc32
container/list
crypto/subtle
time
crypto/cipher
crypto/hmac
compress/bzip2
hash/adler32
container/ring
hash/crc64
hash/fnv
html
image/color
strconv
math/rand
image/color/palette
os
math/cmplx
reflect
crypto
crypto/md5
encoding/base64
regexp/syntax
net/url
path/filepath
crypto/aes
crypto/rc4
io/ioutil
os/exec
crypto/sha1
crypto/sha256
regexp
crypto/sha512
encoding/pem
encoding/binary
fmt
net
crypto/des
os/signal
encoding/ascii85
encoding/base32
image
index/suffixarray
runtime/race
debug/dwarf
debug/gosym
debug/plan9obj
flag
log
go/token
encoding/json
debug/elf
debug/macho
debug/pe
go/scanner
cmd/addr2line
encoding/xml
go/ast
text/template/parse
go/parser
go/printer
compress/flate
text/template
compress/gzip
math/big
encoding/hex
mime
cmd/cgo
go/format
cmd/fix
go/doc
crypto/elliptic
crypto/ecdsa
go/build
crypto/rand
crypto/rsa
crypto/dsa
encoding/asn1
net/textproto
mime/multipart
crypto/x509/pkix
runtime/pprof
crypto/x509
cmd/nm
cmd/gofmt
crypto/tls
cmd/objdump
cmd/pack
cmd/yacc
net/http
archive/tar
archive/zip
compress/lzw
compress/zlib
database/sql/driver
database/sql
encoding/csv
encoding/gob
html/template
image/draw
image/gif
cmd/go
expvar
image/jpeg
image/png
log/syslog
net/http/cgi
net/http/cookiejar
net/http/httptest
net/http/fcgi
net/http/httputil
net/http/pprof
net/mail
net/rpc
net/smtp
os/user
runtime/debug
testing
testing/iotest
net/rpc/jsonrpc
testing/quick
text/scanner
---
Installed Go for linux/amd64 in /usr/local/go
Installed commands in /usr/local/go/bin
fuyundeMacBook-Pro:src fuyun$
编译后BIN目录多了LINUX_AMD64所需要的工具
fuyundeMacBook-Pro:bin fuyun$ pwd
/usr/local/go/bin
fuyundeMacBook-Pro:bin fuyun$ ls
go godoc gofmt linux_amd64 windows_amd64
3、到源代码目录下执行:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
不带前面参数的 go build 只是编译出开发环境适用的执行文件。
注:参考网络资料+实验。有疑问加站长微信联系(非本文作者)