Learning a Programming Language pt 1: Why / Intro to Go Language (90 Days of DevOps)
In this section of the 90 Days of DevOps series, we consider a programming language as a valuable part of the DevOps skill set.
Why learn a programming language?
Since the entire DevOps process is about efficiently producing and delivering quality software, it only makes sense that knowing how to read through some code will be (at the very least) beneficial to those closely involved.
Additionally, familiarity with certain specific programming languages can make it easier to work with and customize common DevOps tooling. At the moment, many popular DevOps tools are built on either Python (e.g. Ansible) or Go (e.g. Docker and Kubernetes), so both of these programming languages are particularly worth exploring.
Why choose Go for DevOps?
The Go language has some pros and cons vs. Python when considering a DevOps use case:
-
Go is statically linked. This keeps deployment of Go programs simple, since a single compiled binary contains everything a Go program needs in order to work on the deployment target. Contrast this to Python, which can require specific libraries to be present on the target before a program will work. Such dependencies increase deployment overhead and can be a source of issues at runtime.
-
Go is a compiled language and known to be performant at runtime. On the other hand, not having to deal with compiling binaries can be considered a plus for Python.
Either or both are good to know in a DevOps context. The “90 Days of DevOps” project opts for Go, so that is what will be covered here.
Go Installation and “Hello, world!”
Go’s official documentation includes a quick tutorial running through the process of installing Go and creating the classic “Hello, world” program.
This post will perform the steps on Linux, but instructions for Mac and Windows are available at the same links above. It assumes basic Linux OS proficiency, and it would be helpful to be aware of functions as a programming concept.
Installation
- Download the latest version of Go from https://go.dev/doc/install
-
Delete any existing Go installation (
/usr/local/go), then extract the downloaded archive to/usr/local:1
# rm -rf /usr/local/go && tar -C /usr/local -xzf DOWNLOADED_FILE.tar.gz
The result should be a fresh
goinstallation in/usr/local/go. - Add
go’s binary directory to your PATH environment variable with the following line in.bashrc,/etc/profile, or similar:1
export PATH=$PATH:/usr/local/go/bin
Hello, world!
-
Make a new project directory and
cdinto it1 2 3
$ cd # change to home directory $ mkdir hello # make new subdirectory under home directory for the "Hello, world" program $ cd hello
-
Enable dependency tracking for this code
1 2
$ go mod init example/hello go: creating new go.mod: module example/hello
The new
go.modfile will keep track of any external module dependencies. -
In your preferred editor, create a file
hello.goin which to write your code. -
Paste the following code into your
hello.gofile and save the file.1 2 3 4 5 6 7 8
package main import "fmt" func main() { fmt.Println("Hello, World!") }
A quick explanation of the Go code above:
package maindeclares that this source file is part of themainpackage. A package in Go is a group of individual functions.import "fmt"imports thefmtpackage (a part of the Go standard library, which contains the function we will use for printing our “hello” message to console).- The following block:
1 2 3
func main() { fmt.Println("Hello, World!") }
declares a function named
main, which takes no input parameters, and returns"Hello, World!"to the console using thePrintlnfunction from thefmtpackage imported earlier. - Compile/run your code to see the greeting.
1 2
$ go run . Hello, World!
So that’s “Hello, world” in Go.
In the next post, we will learn the essentials of the Go programming language, by working through a popular intro course available on the “TechWorld with Nana” YouTube channel.
<< Back to 90 Days of DevOps posts
<<< Back to all posts