Getting Started with Go on Fedora

The Go 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, OpenShift or Terraform 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.

Fedora Project community

11 Comments

  1. hammerhead corvette

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

  2. MX

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

    • 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.

  3. Yazan Al Monshed

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

  4. 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”.

  5. e4ert

    better using crystal
    nice language

  6. 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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions

%d bloggers like this: