golang debian_如何在Debian 10上安装Go

golang debian

介绍 (Introduction)

Go, also known as golang, is a modern, open-source programming language developed by Google. Go tries to make software development safe, fast and approachable to help you build reliable and efficient software.

Go ,也称为golang ,是Google开发的一种现代的开源编程语言。 Go尝试使软件开发安全,快速且易于访问,以帮助您构建可靠,高效的软件。

This tutorial will guide you through downloading and installing Go from source, as well as compiling and executing a “Hello, World!” program on a Debian 10 server.

本教程将指导您从源代码下载和安装Go,以及编译和执行“ Hello,World!”。 Debian 10服务器上的程序。

先决条件 (Prerequisites)

To complete this tutorial, you will need access to a Debian 10 server and a non-root user with sudo privileges, as described in Initial Server Setup with Debian 10.

要完成本教程,您将需要访问Debian 10服务器和具有sudo特权的非root用户,如使用Debian 10进行初始服务器设置中所述。

第1步-下载Go (Step 1 — Downloading Go)

In this step, we’ll install Go on your server.

在此步骤中,我们将在您的服务器上安装Go。

First, ensure your apt package index is up to date using the following command:

首先,使用以下命令确保您的apt软件包索引是最新的:

  • sudo apt update

    sudo apt更新

Now install curl so you will be able to grab the latest Go release:

现在安装curl以便您可以获取最新的Go版本:

  • sudo apt install curl

    sudo apt安装curl

Next, visit the official Go downloads page and find the URL for the current binary release’s tarball. Make sure you copy the link for the latest version that is compatible with a 64-bit architecture.

接下来,访问官方的Go下载页面 ,找到当前二进制发行版压缩包的URL。 确保复制与64位体系结构兼容的最新版本的链接。

From your home directory, use curl to retrieve the tarball:

在主目录中,使用curl来检索tarball:

  • curl -O https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz

    curl -O https://dl.google.com/go/go 1.12.7 .linux-amd64.tar.gz

Although the tarball came from a genuine source, it is best practice to verify both the authenticity and integrity of items downloaded from the internet. This verification method certifies that the file was neither tampered with nor corrupted or damaged during the download process. The sha256sum command produces a unique 256-bit hash:

尽管压缩包来自真实来源,但最佳做法是验证从Internet下载的项目的真实性和完整性。 此验证方法证明文件在下载过程中未被篡改,损坏或损坏。 sha256sum命令产生唯一的256位哈希:

  • sha256sum go1.12.7.linux-amd64.tar.gz

    sha256sum去1.12.7 .linux-amd64.tar.gz


   
   
Output
go1.12.7.linux-amd64.tar.gz 66d83bfb5a9ede000e33c6579a91a29e6b101829ad41fffb5c5bb6c900e109d9 go1.12.7.linux-amd64.tar.gz

Compare the hash in your output to the checksum value on the Go download page. If they match, then it is safe to conclude that the download is legitimate.

将输出中的哈希与Go下载页面上的校验和值进行比较。 如果它们匹配,则可以断定下载是合法的。

With Go downloaded and the integrity of the file validated, let’s proceed with the installation.

下载Go文件并验证文件完整性之后,让我们继续进行安装。

第2步-安装Go (Step 2 — Installing Go)

We’ll now use tar to extract the tarball. The following flags are used to instruct tar how to extract, view, and operate on the downloaded tarball:

现在,我们将使用tar提取tarball。 以下标志用于指示tar如何提取,查看和操作已下载的tarball:

  • The x flag tells it that we want to extract files from a tarball

    x标志告诉我们我们要从压缩包中提取文件

  • The v flag indicates that we want verbose output, including a list of the files being extracted

    v标志指示我们要详细输出,包括要提取的文件的列表

  • The f flag tells tar that we’ll specify a filename to operate on

    f标志告诉tar我们将指定要操作的文件名

Now let’s put things all together and run the command to extract the package:

现在,将所有内容放在一起并运行命令以提取软件包:

  • tar xvf go1.12.7.linux-amd64.tar.gz

    tar xvf go 1.12.7 .linux-amd64.tar.gz

You should now have a directory called go in your home directory. Recursively change the owner and group of this directory to root, and move it to /usr/local:

现在,您的主目录中应该有一个名为go的目录。 将该目录的所有者和组递归更改为root ,然后将其移动到/usr/local

  • sudo chown -R root:root ./go

    sudo chown -R root:根./go
  • sudo mv go /usr/local

    须藤MV去/ usr / local

Note: Although /usr/local/go is the officially-recommended location, some users may prefer or require different paths.

注意:尽管/usr/local/go是官方推荐的位置,但是某些用户可能更喜欢或需要不同的路径。

At this point, using Go would require specifying the full path to its install location in the command line. To make interacting with Go more user-friendly, we will set a few paths.

此时,使用Go将需要在命令行中指定其安装位置的完整路径。 为了使与Go的交互更加用户友好,我们将设置一些路径。

第2步-设置路径 (Step 2 — Setting Go Paths)

In this step, we’ll set some paths in your environment.

在此步骤中,我们将在您的环境中设置一些路径。

First, set Go’s root value, which tells Go where to look for its files:

首先,设置Go的根值,该值告诉Go在哪里寻找其文件:

  • nano ~/.profile

    纳米〜/ .profile

At the end of the file, add the following lines:

在文件末尾,添加以下行:

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

If you chose a different installation location for Go, then you should add the following lines to this file instead of the lines shown above. In this example, we are adding the lines that would be required if you installed Go in your home directory:

如果为Go选择了不同的安装位置,则应在此文件中添加以下行, 而不是上面显示的行。 在此示例中,我们添加了将Go安装在主目录中所需的行:

export GOROOT=$HOME/go
export GOPATH=$HOME/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

With the appropriate lines pasted into your profile, save and close the file.

将适当的行粘贴到您的配置文件中,然后保存并关闭文件。

Next, refresh your profile by running:

接下来,通过运行以下命令刷新您的个人资料:

  • source ~/.profile

    来源〜/ .profile

With the Go installation in place and the necessary environment paths set, let’s confirm that our setup works by composing a short program.

设置好Go安装并设置了必要的环境路径后,让我们编写一个简短的程序来确认我们的设置可以正常工作。

第3步-测试安装 (Step 3 — Testing Your Installation)

Now that Go is installed and the paths are set for your server, you can ensure that Go is working as expected.

现在已经安装了Go并为服务器设置了路径,您可以确保Go可以按预期工作。

Create a new directory for your Go workspace, which is where Go will build its files:

为Go工作区创建一个新目录,Go将在该目录中构建其文件:

  • mkdir $HOME/work

    mkdir $ HOME /工作

Then, create a directory hierarchy in this folder so that you will be able to create your test file. We’ll use the directory my_project as an example:

然后,在此文件夹中创建目录层次结构,以便您能够创建测试文件。 我们将使用目录my_project作为示例:

  • mkdir -p work/src/my_project/hello

    mkdir -p work / src / my_project / hello

Next, you can create a traditional “Hello World” Go file:

接下来,您可以创建传统的“ Hello World” Go文件:

  • nano ~/work/src/my_project/hello/hello.go

    纳米〜/ work / src / my_project /hello/hello.go

Inside your editor, add the following code to the file, which uses the main Go packages, imports the formatted IO content component, and sets a new function to print “Hello, World!” when run:

在编辑器内部,将以下代码添加到该文件中,该文件使用主要的Go软件包,导入格式化的IO内容组件,并设置一个新功能以打印“ Hello,World!”。 运行时:

~/work/src/my_project/hello/hello.go
〜/ work / src / my_project / hello / hello.go
package main

import "fmt"

func main() {
   fmt.Printf("Hello, World!\n")
}

When it runs, this program will print Hello, World!, indicating that Go programs are compiling correctly.

在运行时,该程序将打印Hello, World! ,表明Go程序正在正确编译。

Save and close the file, then compile it by invoking the Go command install:

保存并关闭文件,然后通过调用Go命令install进行编译:

  • go install my_project/hello

    去安装my_project / hello

With the program compiled, you can run it by executing the command:

编译程序后,可以通过执行以下命令来运行它:

  • hello

    你好

Go is successfully installed and functional if you see the following output:

如果您看到以下输出,则Go已成功安装并正常运行:


   
   
Output
Hello, World!

You can determine where the compiled hello binary is installed by using the which command:

您可以使用which命令来确定已编译的hello二进制文件的安装位置:

  • which hello

    你好

   
   
Output
/home/sammy/work/bin/hello

The “Hello, World!” program established that you have a Go development environment.

“你好,世界!” 程序确定您具有Go开发环境。

结论 (Conclusion)

By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. To learn more about working with Go, see our development series How To Code in Go. You can also consult the official documentation on How to Write Go Code.

通过下载并安装最新的Go软件包并设置其路径,您现在拥有了一个用于Go开发的系统。 要了解有关使用Go的更多信息,请参阅我们的开发系列如何在Go中进行编码 。 您还可以参考有关如何编写Go代码的官方文档。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-go-on-debian-10

golang debian

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值