Ubuntu 16.04 安装Go 1.9.2

下载:

官网下载 https://www.golangtc.com/download,选择 Ubuntu 64版本(linux-amd64),我这里下载的是:go1.9.2.linux-amd64.tar.gz

安装:

 #解压至系统目录 (注意权限)
sudo tar -zxvf go1.9.2.linux-amd64.tar.gz -C /usr/local
 #验证

/usr/local/go/bin/go version

设置环境变量

#当前用户
/etc/profile
#系统用户
~/.bashrc

这里选择当前用户:

vim ~/.bashrc

export GOPATH=$HOME/code/golang/ #工作路径
export GOROOT=/usr/local/go  #设置为go安装的路径
export GOARCH=386
export GOOS=linux
export GOBIN=$GOROOT/bin/ #安装可执行文件路径
export GOTOOLS=$GOROOT/pkg/tool/
export PATH=$PATH:$GOBIN:$GOTOOLS

source ~/.bashrc

验证

验证全局变量生效
go env 查看全局变量的设置
go version
验证hello.go
package main
import ("bufio"
        "os"
        "fmt"
      )
func main() {
    fmt.Println("hello world")
    //声明并初始化带缓冲的读取器,从标准输入读取
    inputReader := bufio.NewReader(os.Stdin)
    fmt.Println("Please input your name:")
    //以
为分隔符读取内容
    input,err := inputReader.ReadString('
')
    if err != nil {
        fmt.Printf("Found an error :%s
",err)
    }else {
        //对input进行切片操作,去除最后一个换行符
        input = input [:len(input)-1]
        fmt.Printf("hello,%s!
",input)
    }
}

运行

pp@pp:~/code/golang$ go run hello.go
hello world
Please input your name:
wsq
hello,wsq!
pp@pp:~/code/golang$ go build hello.go
pp@pp:~/code/golang$ ./hello 
hello world
Please input your name:
wsq
hello,wsq!

  

问题:

网上的办法是将 

export GOBIN=$GOROOT/bin/ #安装可执行文件路径

直接注释掉,我这里暂时不适用go install命令吧。

原文地址:https://www.cnblogs.com/shuqingstudy/p/10023559.html