Ubuntu15.10编译安装Golang1.5

前端之家收集整理的这篇文章主要介绍了Ubuntu15.10编译安装Golang1.5前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

安装思路

  • 编译安装go1.4
  • 将go目录名称改为go1.4
  • 修改.bashrc中的环境变量
  • 编译安装go1.5

注:为了构建 Go 1.x (x ≥ 5),需要先安装 Go 1.4 到 $GOROOT_BOOTSTRAP. 默认的 $GOROOT_BOOTSTRAP$HOME/go1.4. 使用 Go 1.4 作为启动的基础版本来编译工具链。

安装Go语言相关工具

sudo apt-get install bison ed gawk gcc libc6-dev make gccgo-5

解压Go语言源码包

$ tar -zxvf go1.4.2.linux-amd64.tar.gz

注:源码包解压后根目录的名字是 go,后面设置环境变量会用到。

配置Go1.4语言环境变量

环境变量的配置不论是下一步中编译Go语言源码,还是在之后的开发过程中都至关重要

$ vim ~/.bashrc
export GOROOT=$HOME/go
export GOROOT_BOOTSTRAP=$HOME/go
export GOARCH=amd64
export GOOS=linux
export GOBIN=$GOROOT/bin/
export GOTOOLS=$GOROOT/pkg/tool/
export PATH=$GOBIN:$GOTOOLS:$PATH
export GOPATH=$HOME/goPro

$ source ~/.bashrc

注:

  • GOROOT是存放源码包的位置。比如,我这里是放在/home/plusgo/目录下;
  • GOROOT_BOOTSTRAP在1.5编译时使用 Go 1.4 的Go compilers进行构建,则需指定go1.4的路径;
  • GOARCH 与 GOOS 与下载的发行版有关,例如:我下载的是64位的Ubuntu,因此 GOARCH 为 amd64;如果是32位的Ubuntu,则填386;而GOOS 为 linux;
  • GOBIN为GOROOT路径下bin文件夹路径;
  • GOTOOLS路径在编译时会用到;
  • PATH路径很关键,指定GOBIN和GOTOOLS路径;
  • GOPATH为Go的工作目录,可以随时修改;这里,我选择将GOPATH指定到/home/plusgo/goPro路径下;则在$HOME目录下新建一个goPro目录($ mkdir goPro)作为Go的工作目录;

编译Go1.4源码

$ cd $GOROOT/src
$ ./all.bash

稍等一会,等待编译完成后,出现下面的内容,则为编译通过:

ALL TESTS PASSED
---
Installed Go for linux/amd64 in /home/plusgo/go
Installed commands in /home/plusgo/go/bin/

修改编译好的go目录

将编译好的go目录改为go1.4目录

$ mv go go1.4

修改.bashrc环境变量

$ vim ~/.bashrc
export GOROOT=$HOME/go
export GOROOT_BOOTSTRAP=$HOME/go1.4
export GOARCH=amd64
export GOOS=linux
export GOBIN=$GOROOT/bin/
export GOTOOLS=$GOROOT/pkg/tool/
export PATH=$GOBIN:$GOTOOLS:$PATH
export GOPATH=$HOME/goPro
 
$ source ~/.bashrc

注:这里将GOROOT_BOOTSTRAP路径指定为已编译好的go1.4目录;

编译Go1.5源码

将下载好的go1.5.1.linux-amd64.tar.gz安装包放到$HOME目录下,并解压,默认为go目录; 进入源码文件夹进行编译:

$ cd $GOROOT/src
$ ./all.bash

稍等一会,等待编译完成后,出现下面的内容,则为编译通过:

ALL TESTS PASSED
---
Installed Go for linux/amd64 in /home/plusgo/go
Installed commands in /home/plusgo/go/bin/

接着输入go目录,如果看到如下信息,则即可使用Go语言了:

$ 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.
原文链接:https://www.f2er.com/ubuntu/354380.html

猜你在找的Ubuntu相关文章