golang 環境搭建
來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。作業系統 ubuntu 12.04 64 位元
1. 配置編譯環境
Go工具鏈採用C語言編寫,需要安裝以下開發工具
$ sudo apt-get install bison gcc libc6-dev ed gawk make
2.安裝Mercurial工具
Mercurial是一種輕量級分布式版本控制系統,採用Python編寫。
首先安裝Python
$ sudo apt-get install python2.7
$ sudo apt-get install python-all-dev
$ sudo apt-get install python-setuptools
安裝Mercurial
$ sudo easy_install -U mercurial
安裝完畢後測試以下
$ hg -v
3. 擷取go源碼
$ hg clone -u release https://go.googlecode.com/hg/ go
4. 編譯源碼
$ cd go/src
$ ./all.bash
5. 編譯成功後配置環境變數
$ sudo gedit ~/.bashrc
添加如下內容:
#golang
export GOROOT=~/go
export GOARCH=amd64
export GOOS=linux
export GOBIN=$GOROOT/bin
export GOTOOL=$GOROOT/pkg/tool/linux_amd64
export PATH=$PATH:$GOBIN:$GOTOOL
其中pkg/tool/linux_amd64是6g 6l工具的位置。
6.測試
test.go檔案 代碼如下
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
編譯、連結、執行
$ 6g test.go
$ ls
test.6 test.go
$ 6l test.6
$ ls
6.out test.6 test.go
$ ./6.out
hello, world
或者直接運行 go run test.go
PS :Go 編譯器支援三種指令集,不同體繫結構產生的程式碼品質有差別:
amd64 (a.k.a.x86-64) 6g 6l 6c 6a 最成熟的實現,編譯器在寄存器層級最佳化,產生高品質的目標代碼。
386 (a.k.a.x86 or x86-32) 8g 8l 8c 8a amd64的完整移植
arm (a.k.a.ARM) 5g 5l 5c 5a
參考:http://wiki.ubuntu.org.cn/Golang
http://code.google.com/p/golang-china/wiki/install