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下刚才设置的环境变量文件。