So, here's the deal. We're not here to throw stones at either Python or Go. Both of these languages are tremendous in their own right, especially when we talk about network automation. So next the question is:
Should I use Python for Network Automation?
or
Should I use Go for Network Automation?
Much like many questions within this domain, the answer isn't black or white. So to best answer these lets first let's look at why most of us, network engineers, are smitten with Python?
Why Python?
With its extensive history and rich ecosystem, Python has naturally become a favourite for many.
Python is dynamically typed, allowing variable types to be determined at runtime. Being interpreted, it may not match the raw performance of compiled languages, but it offers flexibility. Python's centralized package repository, PyPI, boasts an extensive range of libraries. Its error handling is reactive, using try-except
blocks. Python's networking tools are both mature and vast. While it doesn't natively support concurrency like Go, it can achieve parallelism using external libraries.
To summarize, here are some of Python's key benefits:
- Ecosystem: Python is enriched with specialized libraries like Netmiko, NAPALM, Batfish, Scrapli and Nornir—all designed specifically for network tasks.
- Learning Curve: Python's intuitive syntax welcomes beginners, making scripting straightforward and efficient.
- Dynamically Typing: This feature in Python streamlines the coding process, paving the way for quicker automation wins.
$ cat python_loop.py
# Define a list of IP addresses
ip_addresses = ['10.0.0.1', '10.0.0.2', '10.0.0.3']
# Loop through each IP address in the list
for ip in ip_addresses:
# Print the IP address
print(ip)
# To run:
$ python3 python_loop.py
Why Go?
While Python remains popular, Go (or Golang) has been gaining in popularity, especially in environments where performance and efficient deployment are critical.
Go is a statically typed language that requires explicit type declarations and provides compile-time type safety. It's compiled, ensuring faster performance and easy distribution through binaries. Go's package management is distinct, relying on URL imports without a centralized repository. Regarding error handling, Go mandates proactive checks, emphasizing resilience. Its tooling ecosystem, particularly for networking, is rapidly expanding. Notably, Go innately supports concurrency via its goroutines
.
To summarize, here are Go's key benefits:
- Compiled Nature: Go stands out with its capability to compile code into a single executable for various platforms, simplifying deployments.
- Concurrency: Go's inherent concurrency model, powered by goroutines, delivers great performance. However, it does come with the complexity of task synchronization.
- Statically Typed: The need for upfront type declarations in Go minimizes runtime errors, making the code more predictable.
- Error Handling: Go's proactive approach to error checks ensures code resilience from the get-go.
$ cat goloop.go
// Declare the main package
package main
// Import the format package for printing
import "fmt"
// Entry point of the application
func main() {
// Explicitly declare a slice of strings with IP addresses
var ipAddresses []string = []string{"10.0.0.1", "10.0.0.2", "10.0.0.3"}
// Loop through each IP in the slice
for _, ip := range ipAddresses {
// Print the IP address
fmt.Println(ip)
}
}
# To compile:
$ go build goloop.go
# This produces an executable named 'go_loop' (or 'go_loop.exe' on Windows)
# To run:
$ ./goloop (on Linux/Mac)
... or
> goloop.exe (on Windows)
Note: Go requires explicit type declarations, which can ensure type consistency throughout the application.
Comparison Summary
So, to provide a comparison summary:
Go | Python | |
Typed | Statically Typed | Dynamically Typed |
Compiled | Yes | No (Interpreted) |
Package Management | Imports using URLs, no central repo | PyPI |
Error Handling | Proactive (mandatory error checks) | Reactive (try-except blocks) |
Network Tooling | Growing | Extensive & Mature |
Concurrency | Inbuilt (goroutines) | Achievable using external libraries |
Python or Go?
If you're proficient in Python, consider branching out to Go for a new challenge. For those new to programming, I'd recommend starting with Python, but don't hesitate to explore Go afterwards. While Go may have fewer network automation packages, it offers a unique learning experience. For those diving into Go, my courses on Packet Coders (link below) are tailored for Network Engineers, ensuring a relatable and enjoyable learning curve.