Golang的安装
Go的三种安装方式Go有多种安装方式,你可以选择自己喜欢的。这里我们介绍三种最常见的安装方式:
最后,如果你想在同一个系统中安装多个版本的Go,你可以参考第三方工具GVM,这是目前在这方面做得最好的工具,除非你知道怎么处理。 Go源码安装在Go的源代码中,有些部分是用Plan 9 C和AT&T汇编写的,因此假如你要想从源码安装,就必须安装C的编译工具。 在Mac系统中,只要你安装了Xcode,就已经包含了相应的编译工具。 在类Unix系统中,需要安装gcc等工具。例如Ubuntu系统可通过在终端中执行sudo apt-get install gcc libc6-dev来安装编译工具。 在Windows系统中,你需要安装MinGW,然后通过MinGW安装gcc,并设置相应的环境变量。 由于本人平时主要涉及到linux平台下的使用,所以这里只以linux平台为例,不涉及mac 和win下的安装。 Go 源代码下载从 官方页面 、 国内镜像 或github上下载 Go 的源码包到你的计算机上,然后将解压后的目录go通1过命令移动到$GOROOT所指向的位置。 #本人设置$GOROOT为/usr/local/go export $GOROOT=/usr/local/go wget https://storage.googleapis.com/golang/go<VERSION>.src.tar.gz tar zxv go<VERSION>.src.tar.gz mv go $GOROOT 2、安装gcc等编译工具 sudo apt-get install bison ed gawk gcc libc6-dev make 或 yum gcc mak gawk 根据不同的发行版,自行选择相应的编译环境包安装的方式。 3、编译 cd $GOROOT/src ./all.bash 4、环境变量配置 export GOROOT=/usr/local/go export PATH=$PATH:$GOROOT/bin 或者使用如下: export PATH=$PATH:/usr/local/go/bin 根据需要可以在.bashrc、.profile、/etc/profile等任一文件中配置相应的环境变量。 测试直接运行go命令,出现如下部分,表示环境已经安装好。 [root@361way ~]# 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 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 filetype file types gopath GOPATH environment variable 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标准包安装通过官方下的压缩包下载来看,标准包里包含源代码,不过区别的是其解包后已经有相应的bin目录存在,里面有已经编译好的golang二进制包。可以直接使用上面第4部的操作配置环境变量后生效。 第三方工具安装1、centos平台下 需使用第三方源epel ,如下: yum -y install golang 2、ubuntu平台下 sudo apt-get install python-software-properties sudo add-apt-repository ppa:gophers/go sudo apt-get update sudo apt-get install golang-stable git-core mercurial 3、mac平台下 brew update && brew upgrade brew install go brew install git brew install mercurial GVM工具GVM是一个golang虚拟环境配置工具,其允许一台机器上安装多个golang版本,gvm是第三方开发的Go多版本管理工具,类似ruby里面的rvm工具。使用起来相当的方便,安装gvm使用如下命令: bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer) 安装完成后我们就可以安装go了: gvm install go1.4.2 gvm use go1.4.2 也可以使用下面的命令,省去每次调用gvm use的麻烦: gvm use go1.4.2 --default 执行完上面的命令之后GOPATH、GOROOT等环境变量会自动设置好,这样就可以直接使用了。 Go 环境变量Go 开发环境依赖于一些操作系统环境变量,你最好在安装 Go 之间就已经设置好他们。如果你使用的是 Windows 的话,你完全不用进行手动设置,Go 将被默认安装在目录c:/go下。这里列举几个最为重要的环境变量:
Go 编译器支持交叉编译,也就是说你可以在一台机器上构建运行在具有不同操作系统和处理器架构上运行的应用程序,也就是说编写源代码的机器可以和目标机器有完全不同的特性(操作系统与处理器架构)。 为了区分本地机器和目标机器,你可以使用$GOHOSTOS和$GOHOSTARCH设置目标机器的参数,这两个变量只有在进行交叉编译的时候才会用到,如果你不进行显示设置,他们的值会和本地机器($GOOS和$GOARCH)一样。
主机环境使用示例: export GOARCH=amd64 export GOOS=linux hello word创建一个hello.go文件,内容如下: package main import "fmt" func main() { fmt.Printf("hello,worldn") } 运行结果如下: # go run hello.go hello,world 参考页面: golang官方安装页 go语言入门指南 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |