1. 程式人生 > >Ubuntu 16.04 安裝Go 1.9.2

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:") //以\n為分隔符讀取內容 input,err := inputReader.ReadString('\n') if err != nil { fmt.Printf(
"Found an error :%s\n",err) }else { //對input進行切片操作,去除最後一個換行符 input = input [:len(input)-1] fmt.Printf("hello,%s!\n",input) } }

執行

[email protected]:~/code/golang$ go run hello.go
hello world
Please input your name:
wsq
hello,wsq!
[email protected]:~/code/golang$ go build hello.go
[email protected]:~/code/golang$ ./hello 
hello world
Please input your name:
wsq
hello,wsq!

  

問題:

 

網上的辦法是將 

export GOBIN=$GOROOT/bin/ #安裝可執行檔案路徑

直接註釋掉,我這裡暫時不適用go install命令吧。