Go通常称为golang是由Google创建的一种现代开源编程语言,可让您构建可靠且高效的应用程序。许多流行的应用程序,例如Kubernetes,Docker,Prometheus和Terraform都是用Go编写的。

本教程说明如何安装Golang在CentOS 8,包括使用curl命令或者wget命令下载golang的预构建的二进制文件,使用tar命令解压缩golang的压缩包tar.gz,配置环境变量PATH,编写Golang的Hello world示例构建并运行。

安装Go

在撰写本文时,Go的最新稳定版本为1.13.4。在下载压缩包之前,请访问官方的Go 下载页面,并检查是否有可用的新版本。执行以下步骤,以在CentOS 8下载并安装Go。

wgetcurl
wget https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz
tar/usr/local
sudo tar -C /usr/local -xf go1.13.4.linux-amd64.tar.gz
/etc/profile
$HOME/.bash_profile

假设你仅针对当前用户安装Golang,可运行以下echo命令,tee命令和管道组合的脚本来修改PATH环境变量:

echo 'export PATH=$PATH:/usr/local/go/bin' | tee -a ~/.bash_profile
sourcePATH
source ~/.bash_profile

至此,您的CentOS系统上已经安装了Go。

Hello world

GOPATH$HOME/go

要创建Golang的项目,首先创建Go的工作空间目录。请运行以下mkdir命令以创建Go的工作空间:

mkdir ~/go
src/hello
mkdir -p ~/go/src/hello
hello.gohello.go
cd
cd ~/go/
code .
hello.go
package main

import "fmt"

func main() {
    fmt.Printf("Hello, World\n")
}
~/go/src/hellogo build
cd ~/go/src/hello
go build
hellohello
./hello

如果看到以下输出,则说明您已成功安装Go。

Hello, World

结论

现在您已经下载并安装了Go,就可以开始编写您的Go代码。如果您遇到问题或有反馈,请在下面发表评论。