在 Fedora 上开启 Go 语言之旅
Go
![](/data/attachment/album/201909/01/223741e3ltmt9jmiiyayva.jpg)


[Go](https://golang.org/) 编程语言于 2009 年首次公开发布，此后被广泛使用。特别是，Go 已经成为云基础设施领域的一种代表性语言，例如 [Kubernetes](https://kubernetes.io/)、[OpenShift](https://www.openshift.com/) 或 [Terraform](https://www.terraform.io/) 等大型项目都使用了 Go。


Go 越来越受欢迎的原因是性能好、易于编写高并发的程序、语法简单和编译快。


让我们来看看如何在 Fedora 上开始 Go 语言编程吧。


### 在 Fedora 上安装 Go


Fedora 可以通过官方库简单快速地安装 Go 语言。



```
$ sudo dnf install -y golang
$ go version
go version go1.12.7 linux/amd64
```

既然装好了 Go ，让我们来写个简单的程序，编译并运行。


### 第一个 Go 程序


让我们来用 Go 语言写一波 “Hello, World!”。首先创建 `main.go` 文件，然后输入或者拷贝以下内容。



```
package main

import "fmt"

func main() {
     fmt.Println("Hello, World!")
}
```

运行这个程序很简单。



```
$ go run main.go
Hello, World!
```

Go 会在临时目录将 `main.go` 编译成二进制文件并执行，然后删除临时目录。这个命令非常适合在开发过程中快速运行程序，它还凸显了 Go 的编译速度。


编译一个可执行程序就像运行它一样简单。



```
$ go build main.go
$ ./main
Hello, World!
```

### 使用 Go 的模块


Go 1.11 和 1.12 引入了对模块的初步支持。模块可用于管理应用程序的各种依赖包。Go 通过 `go.mod` 和 `go.sum` 这两个文件，显式地定义依赖包的版本。


为了演示如何使用模块，让我们为 `hello world` 程序添加一个依赖。


在更改代码之前，需要初始化模块。



```
$ go mod init helloworld
go: creating new go.mod: module helloworld
$ ls
go.mod main  main.go
```

然后按照以下内容修改 `main.go` 文件。



```
package main

import "github.com/fatih/color"

func main () {
     color.Blue("Hello, World!")
}
```

在修改后的 `main.go` 中，不再使用标准库 `fmt` 来打印 “Hello, World!” ，而是使用第三方库打印出有色字体。


让我们来跑一下新版的程序吧。



```
$ go run main.go
Hello, World!
```

因为程序依赖于 `github.com/fatih/color` 库，它需要在编译前下载所有依赖包。 然后把依赖包都添加到 `go.mod` 中，并将它们的版本号和哈希值记录在 `go.sum` 中。




---


via: <https://fedoramagazine.org/getting-started-with-go-on-fedora/>


作者：[Clément Verna](https://fedoramagazine.org/author/cverna/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[hello-wn](https://github.com/hello-wn) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
The [Go](https://golang.org/) programming language was first publicly announced in 2009, since then the language has become widely adopted. In particular Go has become a reference in the world of cloud infrastructure with big projects like [Kubernetes](https://kubernetes.io/), [OpenShift](https://www.openshift.com/) or [Terraform](https://www.terraform.io/) for example.

Some of the main reasons for Go’s increasing popularity are the performances, the ease to write fast concurrent application, the simplicity of the language and fast compilation time. So let’s see how to get started with Go on Fedora.

## Install Go in Fedora

Fedora provides an easy way to install the Go programming language via the official repository.

$ sudo dnf install -y golang $ go version go version go1.12.7 linux/amd64

Now that Go is installed, let’s write a simple program, compile it and execute it.

## First program in Go

Let’s write the traditional “Hello, World!” program in Go. First create a *main.go* file and type or copy the following.

package main import "fmt" func main() { fmt.Println("Hello, World!") }

Running this program is quite simple.

$ go run main.go Hello, World!

This will build a binary from main.go in a temporary directory, execute the binary, then delete the temporary directory. This command is really great to quickly run the program during development and it also highlights the speed of Go compilation.

Building an executable of the program is as simple as running it.

$ go build main.go $ ./main Hello, World!

## Using Go modules

Go 1.11 and 1.12 introduce preliminary support for modules. Modules are a solution to manage application dependencies. This solution is based on 2 files *go.mod* and *go.sum* used to explicitly define the version of the dependencies.

To show how to use modules, let’s add a dependency to the hello world program.

Before changing the code, the module needs to be initialized.

$ go mod init helloworld go: creating new go.mod: module helloworld $ ls go.mod main main.go

Next modify the main.go file as follow.

package main import "github.com/fatih/color" func main () { color.Blue("Hello, World!") }

In the modified main.go, instead of using the standard library “*fmt*” to print the “Hello, World!”. The application uses an external library which makes it easy to print text in color.

Let’s run this version of the application.

$ go run main.go Hello, World!

Now that the application is depending on the *github.com/fatih/color* library, it needs to download all the dependencies before compiling it. The list of dependencies is then added to *go.mod *and the exact version and commit hash of these dependencies is recorded in *go.sum*.

## hammerhead corvette

Thank you for this post ! I guess, I could do a Getting started with flatpaks or The Rust programming language.

## Clément Verna

Yes please 🙂 that would be great. You can check the contributing guide here https://docs.fedoraproject.org/en-US/fedora-magazine/contributing/

## MX

And now that setting up any go-paths is not needed?

## Clément Verna

If you use modules, yes it is not needed anymore. you can use the go env command to check all the environment variables.

## Robert P. J. Day

It would be massively useful to show how to add Go 1.13-rc1 to an existing Go 1.12 Fedora system, so users can start playing with 1.13.

## Yazan Al Monshed

Thankes you, but that not work on Sliverblue!. How can install it in SB?

## MX

Sorry did not understand the question.

Are you sure to put this from the run in the toolbox?

## Clément Verna

You should probably check Toolbox https://docs.fedoraproject.org/en-US/fedora-silverblue/toolbox/

## DaveO

Caveat – for newbies to golang like me. Don’t create a directory named “go” in you home directory to do your golang programming. You will get error; “$GOPATH/go.mod exists but should not”.

## e4ert

better using crystal

nice language

## leslie Satenstein

I looked at Rust, and it appeared interesting. Rust provides a false sense of security.

It does simple memory management by having the compiler verify when malloc’ed space goes out of scope.

An experienced C programmer, by eyeballing his program, does the same. Generally though, the programmer with Rust will have to verify that the compiler missed some scope rules

The C programmer, due to strong typing will have to verify that his eyeballing of the code or using valgrind, missed some scope rules.

I’ll stick to C.