如何在 Ubuntu 20.04 上安装 Go

什么是围棋?

是静态类型和编译的 谷歌开发的编程语言 并且通常被称为 高朗 由于其域名。 它是由 Rob Pike、Ken Thompson 和 Robert Griesemer 于 2007 年 消除影响编译速度、易于编程和安全的障碍。 在今天的文章中,我们将讨论 Go 的安装要求以及如何在 Ubuntu 20.04 上安装它。

在 Ubuntu 20.04 上安装 Go

要求

  • 服务器:4 GB RAM 和 2 个内核。
  • 操作系统:Ubuntu 20.04。
  • Root 用户:我们以 root 用户身份执行所有命令。 如果您以普通用户身份运行命令,请不要忘记实现 sudo 命令。

安装方法

有两种安装 Go 的方法:使用 Ubuntu 存储库或官方 Golang 套件。

Ubuntu 存储库安装

如果特定版本或环境变量并不重要,最快和最简单的选择是从官方存储库安装。 在安装 Go 之前,最好先使用 apt update && apt upgrade -y 更新系统。

[email protected]:~#  apt update && apt upgrade -y Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required:   gradle-6.8.3 Use 'apt autoremove' to remove it. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following package was automatically installed and is no longer required:   gradle-6.8.3 Use 'apt autoremove' to remove it. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. [email protected]:~#

系统更新后,您可以使用 apt install golang -y 安装 Go。

[email protected]:~# apt install golang -y Reading package lists... Done Building dependency tree        Reading state information... Done Setting up golang-1.13-src (1.13.8-1ubuntu1) ... Setting up golang-1.13-race-detector-runtime (0.0+svn332029-0ubuntu2) ... Setting up libstdc++-9-dev:amd64 (9.3.0-17ubuntu1~20.04) ... Setting up g++-9 (9.3.0-17ubuntu1~20.04) ... Setting up golang-1.13-go (1.13.8-1ubuntu1) ... Setting up g++ (4:9.3.0-1ubuntu2) ... update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode Setting up golang-src (2:1.13~1ubuntu2) ... Setting up golang-race-detector-runtime (2:1.13~1ubuntu2) ... Setting up golang-go (2:1.13~1ubuntu2) ... Setting up golang-1.13-doc (1.13.8-1ubuntu1) ... Setting up golang-1.13 (1.13.8-1ubuntu1) ... Setting up golang-doc (2:1.13~1ubuntu2) ... Setting up golang (2:1.13~1ubuntu2) ... Processing triggers for man-db (2.9.1-1) ... [email protected]:~#

始终验证安装是否成功。

[email protected]:~# go version go version go1.13.8 linux/amd64 [email protected]:~#

由于Go当前版本是go1.16.3,可以看到官方仓库没有安装最新版本。

官方 Golang Suite 安装

现在,我们将回顾如何从官方网站安装 Go。 您可以选择安装最新版本或特定版本的 Go。

下载去 通过选择您喜欢的操作系统。 正如我们刚才所说,最新的稳定版本是go1.16.3,但您也可以下载go1.15.11,因为它仍然是稳定的。

如果您还没有更新包,请更新包,然后下载存档。

[email protected]:~# wget  https://golang.org/dl/go1.16.3.linux-amd64.tar.gz --2021-04-19 21:17:26--  https://golang.org/dl/go1.16.3.linux-amd64.tar.gz Resolving golang.org (golang.org)... 172.217.18.113, 2a00:1450:4001:82b::2011 Connecting to golang.org (golang.org)|172.217.18.113|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://dl.google.com/go/go1.16.3.linux-amd64.tar.gz [following] --2021-04-19 21:17:26--  https://dl.google.com/go/go1.16.3.linux-amd64.tar.gz Resolving dl.google.com (dl.google.com)... 142.250.185.238, 2a00:1450:4001:813::200e Connecting to dl.google.com (dl.google.com)|142.250.185.238|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 129021323 (123M) [application/octet-stream] Saving to: 'go1.16.3.linux-amd64.tar.gz'  go1.16.3.linux-amd64 100%[===================>] 123,04M  10,7MB/s    in 11s       2021-04-19 21:17:38 (10,8 MB/s) - 'go1.16.3.linux-amd64.tar.gz' saved [129021323/129021323]  [email protected]:~#

确保使用 sha256sum 命令验证所有内容都已正确下载。 我们可以验证代码中返回的键值是否与 SHA256 校验和 Golang 下载页面上的值。

[email protected]:~# sha256sum go1.16.3.linux-amd64.tar.gz  951a3c7c6ce4e56ad883f97d9db74d3d6d80d5fec77455c6ada6c1f7ac4776d2  go1.16.3.linux-amd64.tar.gz [email protected]:~#

现在,提取文件。

[email protected]:~# tar xvf go1.16.3.linux-amd64.tar.gz [email protected]:~#

接下来,在您的配置文件中设置路径。

[email protected]:~# nano ~/.profile [email protected]:~#

添加以下值,然后保存。

 GOPATH=$HOME/work  PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

将更改应用到配置文件。

[email protected]:~# source ~/.profile [email protected]:~#

验证 Go 是否已安装并正常工作。

[email protected]:~# go version go version go1.16.3 linux/amd64 [email protected]:~#

使用 Go

让我们检查一个真正的小项目的所有内容。 所有创建的包都必须在 GOPATH 环境变量中指定的文件夹中,这就是我们现在要做的。

使用下面的命令创建一个文件夹。

[email protected]:~# mkdir -p ~/work/go/ [email protected]:~#

现在,导航到您创建的文件夹。

[email protected]:~# cd ~/work/go/ [email protected]:~#

让我们使用 nano 编辑器创建我们的应用程序文件。

[email protected]:~/work/go# nano helloworld.go

下面是对下面代码的每一部分完成的内容的解释:

  • 包主: 声明一个包(包是一种对由一个目录中的所有文件组成的函数进行分组的方法)。
  • 导入“fmt”: 导入 fmt 包,其中包含格式化文本的功能。
  • 函数主(): 运行主函数。
  • fmt.Printf (“Hello, Liquid Web!)n”): 打印一条消息。
package main  import "fmt"  func main() {     fmt.Printf("Hello, Liquid Web!)n") }

使用 build 命令构建程序。

root @ host: ~ / work / go # go build helloworld.go root @ host: ~ / work / go #

使用以下命令运行应用程序。

root @ host: ~ / work / go # ./helloworld Hello, Liquid Web!) root @ host: ~ / work / go #

结论

在本文中,我们介绍了如何在 Ubuntu 20.04 上安装和测试 Go 编程语言。 Go 消除了影响编译速度、易于编程和安全的障碍。

发现 Liquid Web 提供的各种托管解决方案和附加组件,以满足您的需求。