If you're a sysadmin, you probably write a lot of scripts to perform everyday tasks or automate some of your administration work. You probably accomplish this through a shell script using a scripting language such as Bash, or a programming language like Python or Perl.
[ You might also be interested in a sysadmin's guide to Bash scripting. ]
Python and Perl are interpreted languages, which have limitations. Some of these limitations include:
- A particular version of an interpreter must be available both when writing and executing code.
- Dependencies must be available, both at write time and execution time.
- Many errors are seen only at runtime, causing your program to stop or break.
- There can be resource utilization and performance issues for some workloads.
A few years ago, I was working on a project, and I couldn't use Perl due to these limitations. I needed a new language. That's when I found Go.
Benefits of the Go programming language
Go is an open source programming language combining the ease of use provided by dynamic languages with the reliability and performance provided by statically typed, compiled languages. Even though Go is statically typed (meaning variable types are predefined and known at compile time), the Go compiler can infer those for you, so it often feels like you're programming in a dynamic language like Python. This makes Go relatively easy to get started with, but it provides the reliability of type validation, which prevents many common runtime errors seen in dynamic languages.
Compiled Go programs provide resource utilization and performance similar to those written in C, but you don't have to deal with low-level issues like memory management. In addition, Go programs compile to static binaries requiring no dependencies or interpreters at runtime. You can compile your program in your development machine, copy the resulting file to the execution machines, and run it.
Go provides other features, including:
- A mature and comprehensive standard library and third-party packages
- Cross-compilation that lets you write and compile programs for different platforms from a single environment
- Built-in concurrency, so you can use the resources available on your system
- A complete set of tools, including testing, linting, code validation, and completion
Go is the language of choice for many modern tools, including container management tools such as Podman, Kubernetes, and OpenShift.
Due to these features and benefits, Go fills a gap for people who don't program all the time but require a powerful and flexible programming language. Go is a great choice of language for network engineers and sysadmins.
[ Want to test your sysadmin skills? Take a skills assessment today. ]
Install Go
On Linux, use your distribution's package manager to install Go (often named golang
):
$ sudo dnf install golang
Alternatively, you can download and install it from the official Go download webpage. At the time I'm writing this article, the latest version of Go is 1.19.1. To download this specific version, use curl
:
$ curl -sLO https://go.dev/dl/go1.19.1.linux-amd64.tar.gz \
--output-dir /tmp/
Then unarchive this file under /usr/local
. If you've installed Go previously this way, delete the existing installation first:
$ sudo rm -rf /usr/local/go
$ sudo tar -xzvf /tmp/go1.19.1.linux-amd64.tar.gz -C /usr/local/
Finally, add /usr/local/go/bin
to your PATH
by adding the following line to your shell's profile. For example, for Bash, add it to ~/.bash_profile
:
export PATH=/usr/local/go/bin/:$PATH
Reload your terminal profile, open another terminal, or log out and back in to make this setting effective. When the variable is set, test Go by running the go version
command:
$ go version
go version go1.19.1 linux/amd64
Now you're ready to start developing your programs in Go.
[ Keep your favorite Git commands, aliases, and tips close at hand with the Git cheat sheet. ]
Initialize a Go project
To start writing your first Go program, create a folder for your project, and change directory into it:
$ mkdir greet
$ cd greet
Go uses modules to manage a project's dependencies. Generally, a module name has to be unique, so by convention, many developers use the name of their source code repository as the module name. For this example, use the module name example/greet
.
Initialize your new module:
$ go mod init example/greet
go: creating new go.mod: module example/greet
Use packages and imports in Go
Go organizes projects into packages, where each package is a set of related functionalities. To create an executable program, define a package named main
with a function called main()
. This defines the starting point of your program.
Here's the start of a common Go program:
package main
import (
"fmt"
"os"
"os/user"
)
The import
keyword imports functionality from other packages. In this example, you import three packages from the standard library:
fmt
provides input and output functions, including functions to print values onscreen.os
provides an interface to your operating system, providing appropriate exit codes.os/user
allows you to manage system users.
[ Download now: Advanced Linux commands cheat sheet. ]
Use the main function
This sample Go program gets the current user by calling the function user.Current()
from the os/user
package and stores the result in a variable called u
. Then it verifies whether the function call returns an error. If so, it prints an error message and terminates the program. If there's no error, then it prints a greeting message to the current user (by name, thanks to u.Username
).
Here's the code:
func main() {
u, err := user.Current()
if err != nil {
fmt.Println("Cannot get current user:", err)
os.Exit(1)
}
fmt.Printf("Hello %s, welcome!\n", u.Username)
}
Run a Go application
Save the file and run the program dynamically by using the Go tool go run
:
$ go run .
Hello ricardo, welcome!
Compile a Go application
You can also compile a Go module into a single executable binary with go build
:
$ go build
Then run your greet
program by calling it directly:
$ ./greet
Hello ricardo, welcome!
What's next?
You've written, compiled, and executed your first Go program! There is much more you can do. Go is a programming language with clear benefits and features targeted for network engineers and sysadmins. It's a great choice for developing command-line tools and automation solutions.
I've been using Go for many years now, developing several command-line applications, such as log file processing, API clients and servers, network management tools, monitoring tools, data collection, and more. I think Go fits sysadmin work so well that I wrote a book about developing command-line applications with Go. If you're interested in learning more about Go, take a look at Powerful Command-Line Applications in Go.
In the next article in this series, I'll explore some additional concepts from the Go programming language.
About the author
Ricardo Gerardi is Technical Community Advocate for Enable Sysadmin and Enable Architect. He was previously a senior consultant at Red Hat Canada, where he specialized in IT automation with Ansible and OpenShift.
He has been a Linux and open source enthusiast and contributor for over 20 years. He is currently interested in hacking stuff using the Go programming language, and he's the author of Powerful Command-Line Applications in Go: Build Fast and Maintainable Tools. Ricardo also writes regularly about Linux, Vim, and command line tools for Opensource.com and Enable Sysadmin community publications.
Ricardo enjoys spending time with his daughters, reading science fiction books, and playing video games.
More like this
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit