在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 的技巧和提示。敬请关注!
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP