如何在 Rocky Linux 9 上安装 Go

Linux命令 Edge插件网 2年前 (2023-03-10) 612次浏览 已收录 0个评论

Go,也称为Golang,是由Google开发的一种开源编程语言。它以其简单性、效率和可扩展性而闻名。Go 特别适合网络编程和并发编程,它通常用于构建 Web 服务器、分布式系统和其他类型的基于网络的软件。如何在 Rocky Linux 9 上安装 Go

在 Rocky Linux 9 上安装 Go

第 1 步。第一步是将系统更新到最新版本的软件包列表。为此,请运行以下命令:

<span class="pln">sudo dnf check</span><span class="pun">-</span><span class="pln">update
sudo dnf install dnf</span><span class="pun">-</span><span class="pln">utils</span>

第 2 步。在 Rocky Linux 9安装 Go 编程语言。

默认情况下,Golang 在 Rocky Linux 9 AppStream 存储库中不可用。现在运行以下命令,使用命令在 Rocky Linux 系统上下载最新版本的 Golang:wget

<span class="pln">wget https</span><span class="pun">:</span><span class="com">//go.dev/dl/go1.19.5.linux-amd64.tar.gz</span>

完成后,使用以下命令验证下载的压缩包版本:

<span class="pln">sha256sum go1</span><span class="pun">.</span><span class="lit">19.5</span><span class="pun">.</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">amd64</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz</span>

输出:

36519702AE2fd573c9869461990ae550c8c0d955cd28d2827a6b159fda81ff95

接下来,解压缩 Go 压缩包文件:

<span class="pln">tar </span><span class="pun">-</span><span class="pln">zxvf go1</span><span class="pun">.</span><span class="lit">19.5</span><span class="pun">.</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">amd64</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz </span><span class="pun">-</span><span class="pln">C </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span>

第 3 步。配置 Go 语言。

现在轮到维护环境变量了。所有这些都使安装不会在查找命令和编译器时遇到问题:

<span class="pln">echo </span><span class="str">'export GOROOT=/usr/local/go'</span> <span class="pun">|</span><span class="pln"> tee </span><span class="pun">-</span><span class="pln">a </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">profile
echo </span><span class="str">'export PATH=$PATH:/usr/local/go/bin'</span> <span class="pun">|</span><span class="pln"> tee </span><span class="pun">-</span><span class="pln">a </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">profile</span>

最后,使用以下命令刷新您的 bash 配置文件:

<span class="pln">source </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">profile</span>

通过运行以下命令验证 Go 是否已正确安装:

<span class="pln">go version</span>

第 4 步。使用 Go 创建一个示例“hello world”程序。

现在我们创建一个典型的 Hello World 程序示例。首先,创建文件夹:

<span class="pln">mkdir </span><span class="pun">-</span><span class="pln">p project</span><span class="pun">/</span><span class="pln">helloworld
cd project</span><span class="pun">/</span><span class="pln">helloworld</span>

接下来,创建新文件:

<span class="pln">nano helloworld</span><span class="pun">.</span><span class="pln">go</span>

添加以下行:

<span class="kwd">package</span><span class="pln"> main
</span><span class="kwd">import</span> <span class="str">"fmt"</span><span class="pln">
func main</span><span class="pun">()</span> <span class="pun">{</span><span class="pln">
    fmt</span><span class="pun">.</span><span class="typ">Printf</span><span class="pun">(</span><span class="str">"Hi, I am Meilana. Welcome to idroot.us\n"</span><span class="pun">)</span>
<span class="pun">}</span>

保存并关闭文件,然后运行程序:

<span class="pln">go run helloworld</span><span class="pun">.</span><span class="pln">go</span>

感谢您使用本教程在您的 Rocky Linux 9 系统上安装 Go 编程语言。有关其他帮助或有用信息,我们建议您查看 Go 官方网站


Edge插件网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在 Rocky Linux 9 上安装 Go
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址