March 04, 2024
This blog post will cover the basics and usage of the Golang programming language. We’ll also do a comparison with Python to see how they both compare and differ from each other. Lastly, we’ll run through an example of how we can use Golang for network automation against network devices. We’ll assume some basic knowledge of Python to understand the equivalence.
Golang, also known as Go, stands out as a relatively new programming language in comparison to its counterparts. Originating from Google in the late noughties, it has swiftly gained traction in the tech community. Its versatility is evident in its adoption by a many different prominent open-source cloud native projects, including but not limited to Terraform, Prometheus, Grafana, Kubernetes, and Containerlab.
Go allows us to create code that runs fast using little memory, is easy to read and effortless to distribute across different platforms via compiled binaries. Go is a statically typed language, meaning we need to define variable types upfront unlike some other languages. It’s also a compiled language which is distinct from interpreted programming languages. Code is written in a file with extension .go and then compiled into a binary which can be distributed for use. If none of this makes sense right now, don’t fret! We’ll dive in a little deeper shortly.
At this point, some of you may be thinking to yourself, what exactly is wrong with Python and why should I even bother with Go? We’re not here to sling mud at either language, but let’s go through a comparison to help us understand the differences and benefits of each.
The table below provides a summary of the two:
| Python | Golang | |
|---|---|---|
| Easy for beginners | Yes | Involves a learning curve |
| File Extension | .py | .go |
| Compiled/Interpreted | Interpreted | Compiled |
| Typed | Dynamic | Static |
| Packages | PyPI | URLs used in code |
| Network Automation Ecosystem | Mature with large communities | Growing community |
| Concurrency | Supported via external libraries | Built-in |
Python is great as its intuitiveness allows us to quickly achieve our network automation goals with minimal code and is extremely human-readable. On the other hand, Go has a slightly greater learning curve to it and can seem more verbose at first.
As we mentioned Go is a compiled language thus code is turned into a single executable with all relevant dependencies which can be handed around to others who don’t need to go through the faff of installing environments and required libraries as we do with Python. Another benefit of this is that trivial errors are caught early during compilation and before we hit the buggy line of code during execution. Interpreted languages like Python are run line by line at runtime therefore errors only pop up when we actually run it. Although some may argue that having to compile your code each time you make a minor change can feel frustrating at times, especially if it takes a long time to do so.
Go is statically typed meaning all variables must have their type defined (think strings or integers etc.) otherwise your code will not compile. Conversely, Python is dynamically typed meaning variable types are not required but rather optional via type hints. This allows greater flexibility and can make the code feel easier to write.
Golang does not have a centralised package management repository as Python does with PyPI. Instead, we can import non-standard library packages via their GitHub URL, which we’ll see later.
The Python network automation community has been going strong for several years now, with a rich ecosystem of libraries such as Netmiko, Scrapli and Nornir to name a few. Whilst some of these libraries have started to be ported over into Go libraries such as gomiko, scrapligo and gornir, they are less mature than their Python equivalents. It is also fair to say that their communities are smaller.
Let’s see a quick example of code in each language that achieves the same thing. In our scenario here we have a list of IP addresses which we loop over and print out if we find 1.1.1.1.
Python