在Ubuntu上安裝Go 1.7的最佳方法


Go 是一種由 Google 於 2007 年建立的免費開源程式語言。它方便構建簡單、安全、高效的程式。這種語言旨在用於伺服器端程式設計。本文介紹了“如何在 Ubuntu 上安裝 Go 1.7”。

安裝Go程式語言

要下載 Go 語言二進位制歸檔檔案,請使用以下命令:

$ wget https://storage.googleapis.com/golang/go1.7.1.linux-amd64.tar.gz

示例輸出應如下所示:

--2016-12-29 10:49:44-- https://storage.googleapis.com/golang/go1.7.1.linux-amd64.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)... 216.58.197.48, 2404:6800:4007:807::2010
Connecting to storage.googleapis.com (storage.googleapis.com)|216.58.197.48|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 81618401 (78M) [application/x-gzip]
Saving to: ‘go1.7.1.linux-amd64.tar.gz’

go1.7.1.linux-amd64 100%[===================>] 77.84M 5.98MB/s in 16s

2016-12-29 10:50:01 (4.78 MB/s) - ‘go1.7.1.linux-amd64.tar.gz’ saved [81618401/81618401]

要解壓下載的歸檔檔案並將其移動到系統上的目標位置,請使用以下命令:

$ sudo tar -zxvf go1.7.1.linux-amd64.tar.gz -C /usr/local/

示例輸出應如下所示:

go/
go/AUTHORS
go/CONTRIBUTING.md
go/CONTRIBUTORS
go/LICENSE
go/PATENTS
go/README.md
go/VERSION
go/api/
go/api/README
go/api/except.txt
go/api/go1.1.txt
go/api/go1.2.txt
go/api/go1.3.txt
go/api/go1.4.txt
go/api/go1.5.txt
go/api/go1.6.txt
go/api/go1.7.txt
go/api/go1.txt
go/api/next.txt
go/bin/
go/bin/go
go/bin/godoc
go/bin/gofmt
go/blog/
go/blog/content/
go/blog/content/4years-gopher.png
go/blog/content/4years-graph.png
go/blog/content/4years.article
go/blog/content/5years/
go/blog/content/5years/conferences.jpg
go/blog/content/5years/gophers5th.jpg
go/blog/content/5years.article
go/blog/content/6years-gopher.png
go/blog/content/6years.article
go/blog/content/a-conversation-with-the-go-team.article
go/blog/content/advanced-go-concurrency-patterns.article
go/blog/content/appengine-dec2013.article
.......................................................................

設定Go環境

設定Go環境有兩種方法。方法如下所示:

  • 當前會話
  • 永久設定

當前會話

當前會話設定僅適用於當前工作會話。要設定 Go 環境,請按照以下步驟操作:

要設定 GOROOT 環境變數,請使用以下命令:

$ export GOROOT=/usr/local/go

要設定工作位置的 GOPATH,請使用以下命令:

$ export GOPATH=$HOME/tutorialspoint/sample

要設定 PATH 變數以訪問 go 二進位制系統,請使用以下命令:

$ export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

永久設定

永久設定將始終適用。要設定 Go 環境,請按照以下步驟操作:

要開啟 .profile 檔案,請使用以下命令:

$ sudo nano ~/.profile

示例輸出應如下所示:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
...............................................................

現在,在檔案末尾新增以下幾行,如下所示:

export GOROOT=/usr/local/go
export GOPATH=$HOME/tutorialspoint/sample
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

現在儲存並關閉檔案。要重新整理配置檔案,請使用以下命令:

$ source ~/.profile

要驗證 go 版本,請使用以下命令:

$ go version

示例輸出應如下所示:

go version go1.7.1 linux/amd64

要驗證所有已配置的環境變數,請使用以下命令:

$ go env

示例輸出應如下所示:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/tutorialspoint/tutorialspoint/sample"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build771783301=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

閱讀本文後,您將能夠了解如何在 Ubuntu 16.04 上安裝 Go 1.7。在我們的後續文章中,我們將提供更多基於 Linux 的技巧和提示。敬請關注!

更新於:2020年1月20日

551 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.