Harder than scripting, but easier than programming in C
Programming Snapshot – Golang
Released back in 2012, Go flew under the radar for a long time until showcase projects such as Docker pushed its popularity. Today, Go has become the language of choice of many system programmers.
In 2012, Unix and C veterans Robert Griesemer, Rob Pike, and Ken Thompson released the system-oriented programming language Go under the aegis of Google. For a long time it eked out a niche existence, before eventually becoming the industry standard for system-oriented programming. Today, observers of the Unix scene are rubbing their eyes in disbelief over the number of tools developed in Go.
To name a programming language after an everyday word such as Go seems like a pretty crazy idea from the viewpoint of a search engine operator. After all, search engines actually remove filler words such as "go" from incoming queries. So, when looking for Go programming tips, the recommendation is to search for "Golang" instead, which has also become the accepted name for the language in the community.
Quickly Installed
If you want to try Go, the easiest approach is to grab a package for your favorite distro. On Ubuntu, for example, type:
sudo apt install golang
After the install, go build
gives you a super-fast Go compiler; gofmt
a pretty printer; go doc
a renderer for manual pages, an extensive core library, and much more.
Go offers a mature development framework, a huge standard library for handling typical programming tasks, support for automatic testing, and a lively community that keeps uploading new libraries to GitHub, from which you can easily include them in your own applications via simple references in your own code.
In the following exploration, I'm going to present only a limited number of useful language features; many may sound familiar, as you might have heard of them in typical "language wars" in the vein of endless discussions such as "Emacs vs. vi". If you want to learn more, I recommend Go's online interactive tutorial [1] or the excellent original book written by one of the Go makers [2].
gofmt
The Internet community has wasted a good deal of energy over the years infinitely discussing the right number of indentations or spaces between keywords or even the "right" editor.
Following Python's strict example to some extent, the Go community prefers a very specific code format. The compiler doesn't grumble if someone now uses four or eight spaces for indentation instead of tabs, but the community insists on principle that any code is run through the gofmt
prettyfier first before it appears anywhere online in a repository such as GitHub. The formatter rigorously sets tabs for indentation and removes spaces between round brackets and text, but puts spaces around punctuation such as +
or =
for easier reading. There is no arguing with this; it is just the way things are done.
The situation is similar for CamelCase within variables and functions: geoSearch()
is in; geo_search()
is out. By the way, it also matters whether you start a variable with upper or lowercase. In a structure or package, Go exports uppercase variables outside the current context, and lowercase variables remain private.
Result or Error?
One hot topic in programming languages is the pros and cons of exceptions (Java, Python) versus returned error codes. Go sees itself in the tradition of the classics like C (which is understandable given Go's list of authors) and evaluates return values with each function call – but with a twist. Since Go functions can return several values, an error code usually comes back along with a result, like with the ReadFile()
function from the Go os
standard library, for example (see Listing 1 [3]).
Listing 1
readfile.go
package main import ( "log" "os" ) func main() { data, err := os.ReadFile("/tmp/dat") if err != nil { log.Fatal(err) } os.Stdout.Write(data) }
The first return value, data
, contains the data found inside the specified file, as an array of the type []byte
after a successful read operation. However, if something goes wrong, an error is returned as the second value in the err
variable. The main program code checks this result as instructed with err != nil
. This returns a true value for the if
condition in case of an error because, in case of success, err
is set to nil
.
By the way, in case of an error, you should not use the other return value (data
). Usually, it is set to its so-called null value in Go anyway, which variables assume after they are declared but not yet initialized. In the example of an array of type []byte
, this is an empty array.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.