avatarTony

Summary

The provided web content introduces DevOps engineers to programming in Go, detailing the language's advantages, installation process, and how to write and run a basic "Hello, world" program.

Abstract

The article emphasizes the growing importance of Go for DevOps, citing its reliability and scalability as key reasons for its adoption in cloud-native applications. It contrasts Go's stability and ease of maintenance with Python's compatibility issues during version upgrades. The Go team's commitment to biannual releases and support for the latest two stable major versions ensures a consistent development environment. The article guides readers through installing Go version 1.19.3 on a Linux server, setting up the development environment, and creating a simple Go program. It also recommends using GoLand or Visual Studio Code as Go IDEs, with a preference for the latter due to its cost-effectiveness.

Opinions

  • The author believes that Go is becoming the de facto language for cloud-native applications and DevOps tooling, suggesting that Python's compatibility issues make Go a more suitable choice for the cloud era.
  • The author expresses confidence in the stability and quality of Go's releases, advising users to adopt the latest version without significant concerns about support or major bugs.
  • There is an opinion that mastering Go will help DevOps engineers stay competitive in their careers and better manage new cloud-native applications.
  • The author recommends Visual Studio Code over GoLand for practical reasons, including cost savings, despite acknowledging GoLand's capabilities as a Go IDE.

DevOps in Go — Your First Go Program

DevOps in Go bootcamp series

Why DevOps in Go?

Currently, most of DevOps engineers use Python as their DevOps programming language. But Python came with some compile-time and service-scaling issues.

For example, simply upgrade to a new version of Python (Python2 to Python3, Python3.7 to Python3.11) might cause your existing script to stop working. When compatibility issue happens, it is not easy to roll back to an old version of Python.

Now in cloud era, Go has become the de facto language for cloud native orchestration and applications. Go comes with all the tools you need to make huge strides in the reliability of your tooling and ability to scale.

A quick glance on the Go official website, you can see the following company (and many more) already using Go (https://golang.google.cn/solutions/#case-studies):

Plus, Docker-compose , kubernetes , istio , k3s , prometheus , argocd , terraform , etc… You can see more and more cloud softwares are written in Go as well. As a DevOps Engineer, personally I think it is important to adopt Go as a DevOps programming language, it will help you mastering new cloud native applications, as well as enable you to remain competitive in your career.

Go Version

Today, the Go team has stabilized the release rhythm of the version at two major releases a year, usually February and August. The Go team promises to provide support for the latest two stable major versions of Go.

For example, if the latest major version is Go 1.17, then the Go team will provide support for Go 1.17 and Go 1.16. The scope of support mainly includes repairing major issues in the version, document changes, and security issues updates.

I recommend you to choose the latest Go version. Because the average quality of the stable version of the Go has always been very high, there are few major bugs that affect the use. You don’t have to worry too much about the support of the new version. Google’s own products, such as Google App Engine, will support it very quickly.

Install Go v1.19.3 on Linux server

Go supports almost all mainstream Linux distribution operating systems. Common ones include Ubuntu, CentOS, Fedora, SUSE, etc. The installation methods of Go on these mainstream Linux distribution operating systems are the same (of course, a certain distribution may also use its software installation manager to provide only its own installation method). You can refer to the following installation steps (https://golang.org/doc/install)

  • Download Go v1.19.3 from its official website:
$ wget https://go.dev/dl/go1.19.3.linux-amd64.tar.gz                                                                                               --2022-11-08 08:12:19--  https://go.dev/dl/go1.19.3.linux-amd64.tar.gz
...
Saving to: ‘go1.19.3.linux-amd64.tar.gz’
go1.19.3.linux-amd64.tar.gz                100%[======================================================================================>] 142.01M  13.2MB/s    in 11s
2022-11-08 08:12:30 (13.2 MB/s) - ‘go1.19.3.linux-amd64.tar.gz’ saved [148907134/148907134]
  • Install Go binary to “/usr/local/bin”
$ rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.3.linux-amd64.tar.gz
$ ls /usr/local
bin  etc  games  go  include  lib  lib64  libexec  sbin  share  src
  • Add “/usr/local/go/bin” into your .bashrc if you have done this already
$ echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
  • Check Go version
$ which go
/usr/local/go/bin/go
$ go version
go version go1.19.3 linux/amd64

Go Hello world

Now we have Go installed successfully, let’s create our first Go program!

  • Create a godev folder under home directory
$ mkdir godev
  • Create helloworld folder in godev
$ cd godev
$ mkdir helloworld
$ cd helloworld
  • Create main.go file with the following content (Note that in Go , we usually concatenate multiple words directly as source file name, instead of using other separators such as underscores. This is due to the fact that underscores has a special role in Go).
package main
import "fmt"
func main() {
        fmt.Println("Hello, world")
}
  • Run it:
$ go fmt main.go
$ go run main.go
Hello, world

Congratulations, you just successfully run your first go helloworld program!

Source Code Analysis

Now that you have successfully ran you first Go program, let’s go through each line

package main

This defines a package in Go. A package is the basic unit of Go language and a Go program is essentially a collection of packages.

In our example, it tells the Go compiler to create an executable as opposed to a library file. If you name the package differently, you will run int the following error:

package command-line-arguments is not a main package

For example, if you rename it to apple, as follows:

package apple
import "fmt"
func main() {
        fmt.Println("Hello, world")
}

and if you run go run main.go:

$ go run main.go
package command-line-arguments is not a main package

However you can still build it, but it will not generate any executables:

$ go build main.go
$ ls
main.go

import

As the name suggests, this import keyword imports the specified package from the directory of $GOPATH (if no path is mentioned) or else from the mentioned directory.

In our example, fmt is the name of a package, it also represents the import path of the package, which is the fmt directory under the standard library.

func

func is a keyword in Go , which is used to create a function. The syntax looks like:

func function_name(Parameter-list)(Return_type){
    // function body.....
}

The main function here is special: when you run an executable Go program, all the code will start running from this entry function.

fmt.Println

fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Println() function uses the default formats for its operands and writes to standard output. A newline is appended at the end.

Go IDE

I recommend you use GoLand or VsCode. But GoLand costs you $19.90 monthly or $199.00 yearly. VsCode is free and that’s what I use. To setup Go with VsCode, you can just follow the VsCode official documentation: https://code.visualstudio.com/docs/languages/go

Golang
DevOps
Cloud Computing
Programming
Recommended from ReadMedium