PC環境:Ubuntu 15.10 64位機
先看張這個月的編程語言排行榜,會發現C下降的特別快,而移動應用和web編程語言都在逐漸上升,其中go上升最明顯。所以我決定在學習java、android的同時也準備學go。給自己加油。
Go安裝
Go支持三種安裝方式:Go源碼安裝、Go標準包安裝、第三方工具安裝。
這裏只講下標準安裝。
下載
訪問https://golang.org/dl/,下載go1.6.3.linux-amd64.tar.gz。題外話:如果不是64位系統,強烈建議更換成64位操作系統。
安裝
我一般將開發環境安裝在根目錄下的opt目錄。
tar -zxvf go1.6.3.linux-amd64.tar.gz -C /opt/go
環境設置
需要設置GOROOT、GOBIN、GOPATH還有PATH。
export GOROOT=/opt/go
export PATH=$PATH:$GOROOT/bin/
export GOPATH=~/goWorkProjects/goPath
export GOBIN=$GOPATH/bin
這幾個一定要配置,否則有些go命令會因爲GOBIN沒有配置而不支持。GOPATH允許多個目錄,採用分隔符分開(linux是冒號)。這些配置我通常將他們寫入~/.bashrc
文件,也可以/etc/profile
或~/.profile
。這個看個人 喜好了。
根據GO的約定,GOPATH【工作目錄】需要建立3個目錄:
- bin(存放編譯後生成的可執行文件)
- pkg(存放編譯後生成的包文件)
- src(存放項目源碼)
自此,go的環境安裝好了,怎麼驗證呢,打開終端,執行go
jindg@nc:~$ go
Go is a tool for managing Go source code.
Usage:
go command [arguments]
The commands are:
build compile packages and dependencies
clean remove object files
doc show documentation for package or symbol
env print Go environment information
fix run go tool fix on packages
fmt run gofmt on package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet run go tool vet on packages
Use "go help [command]" for more information about a command.
Additional help topics:
c calling between Go and C
buildmode description of build modes
filetype file types
gopath GOPATH environment variable
environment environment variables
importpath import path syntax
packages description of package lists
testflag description of testing flags
testfunc description of testing functions
Use "go help [topic]" for more information about that topic.
看到go的Usage信息,就說明go已經安裝成功了。如果命令不存在,檢查下自己對PATH環境變量或source下剛纔設置的環境變量文件。