這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
$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
2、下載並安裝GO1.3
- go1.3.darwin-amd64-osx10.8.pkg
執行下面命令:
$ 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 只是編譯出開發環境適用的執行檔案。
註:參考網路資料+實驗。