Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Network Automation with Go
Network Automation with Go

Network Automation with Go: Learn how to automate network operations and build applications using the Go programming language

Arrow left icon
Profile Icon Nicolas Leiva Profile Icon Michael Kashin
Arrow right icon
AU$61.99 AU$88.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (5 Ratings)
eBook Jan 2023 442 pages 1st Edition
eBook
AU$61.99 AU$88.99
Paperback
AU$110.99
Subscription
Free Trial
Renews at AU$24.99p/m
Arrow left icon
Profile Icon Nicolas Leiva Profile Icon Michael Kashin
Arrow right icon
AU$61.99 AU$88.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (5 Ratings)
eBook Jan 2023 442 pages 1st Edition
eBook
AU$61.99 AU$88.99
Paperback
AU$110.99
Subscription
Free Trial
Renews at AU$24.99p/m
eBook
AU$61.99 AU$88.99
Paperback
AU$110.99
Subscription
Free Trial
Renews at AU$24.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Network Automation with Go

Introduction

Go has emerged as one of the top three most wanted programming languages according to the Stack Overflow Developer Survey 2021 (Further reading), and it has become the preferred choice for writing cloud-native applications such as Kubernetes, Docker, Istio, Prometheus, and Grafana.

Despite this, we still don't see this trend manifest in the network engineering community, where fewer than 20% of network engineers saying they currently use Go for their network automation projects, according to the NetDevOps 2020 survey (Further reading), even though 41% of Go developers say they use Go for network programming in Go Developer Survey 2020 Results (Further reading).

This book strives to address this disparity by offering a practical introduction to Go and network automation for network engineers who want to evolve network management and operation using Go, and software engineers wanting to get into network infrastructure automation. We also hope that this book may be useful to network automation engineers who know and use Python today but want to expand their skill set with a different programming language.

We start by discussing the benefits of Go from different angles and how they apply to the networking field. By the end of this chapter, you should have a good understanding of the main aspects of Go and how to get Go installed on your computer to follow along with the code examples.

In this first chapter, we cover the following topics:

  • Networking and Go
  • Why Go?
  • The future of Go
  • Go versus Python
  • Installing Go on your computer

Technical requirements

We assume basic familiarity with the command line, Git, and GitHub. You can find the code examples for this chapter in the book's GitHub repository (https://github.com/PacktPublishing/Network-Automation-with-Go), under the ch01 folder.

To run the examples, proceed as follows:

  1. Install Go 1.17 or later for your operating system. You can follow the instructions in the Installing Go on your computer section of this chapter or go to https://go.dev/doc/install.
  2. Clone the book's GitHub repository with git clone https://github.com/PacktPublishing/Network-Automation-with-Go.git.
  3. Change the directory to an example's folder with cd Network-Automation-with-Go/ch01/concurrency.
  4. Execute go run main.go.

Networking and Go

Go is widely used in generic infrastructure software—from workload orchestration (Docker and Kubernetes), through telemetry and monitoring (Prometheus and Grafana), all the way to automation tooling (Terraform and Vagrant).

Networking is not the exception—some notable networking projects using Go include Container Network Interface (CNI) plugins such as Cilium or Calico, routing protocol daemons such as GoBGP and Bio-RD, virtual private network (VPN) software such as Tailscale, and most of OpenConfig's ecosystem, including projects such as gRPC Network Management Interface (gNMI) and goyang.

Other use cases include cloud and network services, command-line interfaces (CLIs), web development, development-operations (DevOps), and site reliability.

Go is a programming language the Go founders created to address modern challenges such as multi-core processing, distributed systems, and large-scale software development from day one.

Go's built-in first-class concurrency mechanisms make it an ideal choice for long-lived low-bandwidth input/output (I/O) operations, which are typical requirements of network automation and network operations applications.

What makes the Go language so appealing to software developers? Why, out of all the programming languages out there, should you invest time in learning Go? This is what we address in the next section.

Why Go?

When choosing which programming language to learn next, most people focus mainly on technical reasons. We believe that the choice can be a bit more nuanced, so we try to approach this question from different angles. We start with non-technical arguments, something that's often overlooked but that we believe is important and can have a major impact on both the learning process and day-to-day use. Following that, we cover generic technical arguments that help Go stand out in the very competitive landscape of modern programming languages. We close out this section by exploring different facets of Go that can benefit people, specifically in the fields of networking and network automation.

Non-technical reasons

Whether you are new to the language or have some experience with it, you can access more experienced Go developers in the community who are willing to help you learn more about the language. We include some pointers to community resources and go through the adoption and popularity of Go.

Last but not least, we want to address the maturity of the language, whether it's still in development, and where Go is headed in the future.

Community

A healthy community is almost always an attribute of a successful project. The Go programming language is no exception, with its welcoming and growing community of Go developers—Gophers, with about 2 million of them in the world, according to Russ Cox's article, How Many Go Developers Are There? (Further reading). You can see Renée French's Go Gopher mascot here:

Figure 1.1 – Go Gopher, by Renée French

Figure 1.1 – Go Gopher, by Renée French

The Go user community has several places where newcomers can ask questions and get help from more experienced Go developers, as listed here:

  • golang-nuts mailing list (Further reading)—Google Groups mailing list for any generic language discussions
  • Go Forum (Further reading)—a standalone forum for technical discussions, release announcements, and community updates
  • Go Language Collective (Further reading)—the official question-and-answer (Q&A) channel on Stack Overflow
  • Gophers Slack channel (Further reading)—a place for generic and topic-specific discussions, including a dedicated networking channel

If you want more live interactions, there are some options available as well, as outlined here:

  • A good deal of in-person meetups are available via the Go Developers Network (GDN) (Further reading).
  • One of the principal events in the Go community is GopherCon, held regularly in different parts of the world.
  • The official Go wiki page hosted on GitHub keeps track of all future and past Go conferences and major events (Further reading).

Popularity

Ever since its foundation in the late 2000s, Go has gained a lot of interest from the developer community, not least because of who was behind it. Developed by a group of some of the best computer scientists employed by Google to solve the problems of C/C++, Go is a language that's both simple to understand and nearly as efficient as its predecessors. It took a few years to mature, but it had soon become the new hot start up language, and many up-and-coming software companies such as Docker and HashiCorp adopted it.

Most recently, the Stack Overflow Developer Survey 2021 (Further reading) recognized Go as one of the top three most wanted programming languages by developers. Continuous support from its mothership, and the success of Kubernetes, have made it a de facto standard language to write cloud-native applications with such notable projects as Istio, CoreDNS, Prometheus, and Grafana. As more and more users adopt these applications, it's hard to imagine Go's popularity waning in the future.

Here are a few extra data points in support of Go's rising popularity that are worth mentioning:

  • 225 out of 291 Cloud Native Computing Foundation (CNCF) projects use Go, as reported in the CNCF DevStats toolset (Further reading).
  • Go ranks third as the language with the most stars on GitHub, according to GitHut 2.0 (Further reading).
  • Go is behind three out of the four most popular development tools (Docker, Kubernetes, and Terraform) (Further reading).
  • Go is in the top 10 of the Stack Overflow Developer Survey 2021's top-paying technologies ranking (Further reading).

Maturity

While the Go team released Go (version 1) not too long ago (March 2012), Go has been getting minor changes ever since. The language designers assume a strict position against adding unnecessary features that may result in feature creep. At GopherCon 2014's opening keynote, Rob Pike made this comment explicitly: "The language is done." Russ Cox also mentioned this in his article Go, Open Source, Community (Further reading), referring to Go 1 specifically.

This doesn't mean Go does not have its fair share of pain points. For example, dependency management has been a problem the Go team addressed fairly recently with the introduction of Go modules to better group Go packages you release together. There was also a lack of generics support, a feature that the Go team is now introducing in Go 1.18, probably the most significant change since the release of Go (version 1). Now, users can represent functions and data structures with generic types, which enables code reuse. This addresses one of the primary requests from the community, as Go Developer Survey 2020 Results shows (Further reading).

Despite that, these few changes are very selective and designed to dramatically improve developer productivity. It's safe to assume that we won't see a situation where you have to learn new language concepts and idioms every year and have to rewrite your code to maintain forward compatibility. The Go 1 compatibility guarantee in Go 1 and the Future of Go Programs (Further reading) states the following:

It is intended that programs written to the Go 1 specification will continue to compile and run correctly, unchanged, over the lifetime of that specification. ...code that runs under Go 1.2 should be compatible with Go 1.2.1, Go 1.3, Go 1.4, and so on.

Go benefits from the lessons learned from other programming languages. Pascal, Oberon, C, and Newsqueak are among the languages that influenced Go. We explore their impact in Chapter 2, Go Basics.

Go follows a 6-month release cycle (Further reading). In the Go release notes for each version (Further reading), there is a section at the top that describes changes to the language, which in general is very brief or empty. Over the last couple of years, they reported only four small enhancements to the language, which is a good sign of maturity.

How much Go will change in the future is something we discuss in the next section.

The future of Go

The success of Go version 1 has attracted a lot of developers, most of them with prior experience in other languages that helped shape their mindset and expectations of what a programming language should deliver. The Go team has defined a process to propose, document, and implement changes to Go (Further reading), to give a way for these new contributors to voice their opinions and influence the design of the language. They would label any proposals that break the language-compatibility guarantee, described in the preceding section, as Go 2.

The Go team announced the start of the process of developing Go version 2 at GopherCon 2017 and with the blog post Go 2, here we come! (Further reading). The intention is to ensure the language continues to enable programmers to develop large-scale systems, and to scale to a sizable code base that big teams work on simultaneously. In Toward Go 2 (Further reading), Russ Cox said the following:

Our goal for Go 2 is to fix the most significant ways Go fails to scale.

Any language change proposal needs to follow the Go 2 language change template (Further reading). They are shipping all Go 2 features that are backward-compatible incrementally in Go 1. After that is complete, they can introduce backward-incompatible changes (see Go 2 proposals: Further reading), in case they offer a significant benefit, into Go 2.0.

Support for generic data types is part of the Go 2 draft designs document (Further reading), along with improved error handling, and error-value semantics. The first implementation of generics has already made it into Go 1. The other items in the list are still under evaluation, pushing the release of 2.0 further into the future.

Technical reasons

Go's build speed is a top-of-the-chart aspect of Go that Go developers are more satisfied with, according to Go Developer Survey 2020 Results (Further reading). It's followed very closely by Go's reliability, in second place.

The list of technical aspects we could highlight is large, but aside from build speed and reliability, we cover performance, cross-compiling, readability, and Go's tooling.

Type safety

Most programming languages can be broadly categorized as either statically typed when variable types are checked at compile time or dynamically typed when this check happens during the program execution (runtime). Go belongs to the first category and requires programs to declare all variable types explicitly. Some beginners or people with a background in dynamically typed languages might see this as a detractor.

Type declarations increase the amount of code that you need to write, but in return, you not only get performance benefits but also protection from type errors occurring at runtime, which can be a source of many subtle and hard-to-troubleshoot bugs. For example, consider the program in the next code example at https://github.com/PacktPublishing/Network-Automation-with-Go/blob/main/ch01/type-safety/main.go:

func process(s string) string {
    return "Hello " + s
}
func main() {
    result := process(42)
}

A process function takes a string data type as input and returns another string that concatenates Hello and the value of the input string. A dynamically typed program can crash if this function receives a value of a type different from string, such as an integer, for example.

These errors are very common, especially when dealing with complex data structures that can represent a network configuration or state. Go's static type checking prevents the compiler from producing a working binary generating the following error:

cannot use 42 (type untyped int) as type string in argument to process

Readability also improves with Go's static typing. A developer might be able to keep the entire data model in mind when writing code from scratch, but as new users come into a project, code readability becomes critical to help them understand the logic to make their required code changes. No longer do they need to guess which value type a variable stores—everything is explicitly defined by the program. This feature is so valuable that some dynamically typed languages forgo the benefit of their brevity to introduce the support for type annotations (such as Python typing: Further reading), with the only goal to help integrated development environments (IDEs) and static linters catch obvious type errors.

Go builds are fast

Go is a compiled language that creates small binary files in seconds or a couple of minutes tops. Initial build time may be a bit longer, mostly because of the time it takes to download dependencies, generate extra code, and do other household activities. Subsequent builds run in a fraction of that time. For example, the next capture shows that it takes no more than 10 seconds to rebuild a 120-megabytes (MB) Kubernetes application programming interface (API) server binary:

$ time make kube-apiserver
+++ [0914 21:46:32] Building go targets for linux/amd64:
    cmd/kube-apiserver
> static build CGO_ENABLED=0: k8s.io/kubernetes/cmd/kube-apiserver
make kube-apiserver  10.26s user 2.25s system 155% cpu 8.041 total

This allows you to iterate quickly through the development process and to keep focus, without spending minutes waiting for code to recompile. Some developer productivity tools, such as Tilt, take further actions to optimize the development workflow so that it takes seconds for changes to propagate from a developer's IDE to their local staging environment.

Reliability

Let's define this term as a set of properties of a programming language that help developers write programs that are less likely to fail because of bugs and other failure conditions, as Jiantao Pan from Carnegie Mellon University (CMU) describes in Software Reliability (Further reading). This is one of Go's core tenets, as its website (Further reading) highlights:

Build fast, reliable, and efficient software at scale.

Go developers also say reliability is the second aspect of Go they are most satisfied with, only behind build speed, based on Go Developer Survey 2020 Results (Further reading).

A more reliable software means less time spent chasing bugs and more time invested in the design and development of extra features. We've tried to put together a set of features that we think contribute to increased program reliability. This is not a definitive list, though, as interpretation and attribution of such features can be very subjective. Here are the features we've included:

  • Code complexity—Go is a minimalistic language by design. This translates into simpler and less error-prone code.
  • Language stability—Go comes with strong compatibility guarantees, and the design team tries to limit the number and impact of newly added features.
  • Memory safety—Go prevents unsafe memory access, which is a common source of bugs and exploits in languages with pointer arithmetic, such as C and C++.
  • Static typing—Compile-time type-safety checks catch many common bugs that would otherwise go unnoticed in dynamically typed languages.
  • Static analysis—An automatic way to analyze and report several errors, such as unused variables or unreachable code paths, comes built into the language tooling with go vet.

Performance

Go is a highly performant language. The Computer Language Benchmarks Game (Further reading) shows that its performance is in the vein of languages with manual memory management, such as C/C++ and Rust, and that it offers considerably better performance than dynamic type languages such as Python and Ruby.

It has native support for multi-core multithreaded central processing unit (CPU) architectures, allowing it to scale beyond a single thread and to optimize the use of CPU caches.

Go's built-in garbage collector helps you keep the memory footprint of your program low, and Go's explicit type declaration optimizes memory management and storage of values.

The Go runtime gives you profiling data, which you can visualize with pprof to help you hunt for memory leaks or spot bottlenecks in your program and fine-tune your code to achieve better performance and optimize resource utilization.

For more details on this subject, we recommend checking out Dave Cheney's Five things that make Go fast blog post (Further reading).

Cross-platform compiling

Go can natively produce binaries for different target architectures and operating systems. At the time of writing, the go tool dist list command returns 45 unique combinations with operating systems ranging from Android to Windows and instruction sets that go from PowerPC to ARM. You can change the default values inherited from the underlying operating system and architecture with GOOS and GOARCH environment variables.

You can build an operating system-native version of your favorite tool written in Go, regardless of which operating system you are currently on, as illustrated in the following code snippet:

ch01/hello-world$ GOOS=windows GOARCH=amd64 go build
ch01/hello-world$ ls hello-world*
hello-world.exe

The preceding output shows an example to create a Windows executable on a Linux machine.

Readability

This is, arguably, one of the best qualities of Go when compared to other high-performance languages such as C or C++. The Go programming language specification (Further reading) is relatively short, with around 90 pages (when other language specifications can span over 1,000 pages). It includes only 25 keywords, with only one for loop (for). The number of features is intentionally low to aid code clarity and to prevent people from developing too many language idioms or best practices.

Code formatting is an active battleground in other languages, while Go prevented this problem early on by shipping automatic opinionated formatting as part of the go command. A single run of go fmt on any unformatted (but syntactically correct) code updates the source file with the right amount of indentation and line breaks. This way, all Go programs have a similar look, which improves readability by reducing the number of personal style preferences in code.

Some might say that explicit type declarations alone improve code readability, but Go takes this a step further by making comments an integral part of the code documentation. All commented lines preceding any function, type, or variable declaration gets parsed by the go doc tool website (Further reading) or an IDE to autogenerate code documentation, as the following screenshot shows:

Figure 1.2 – Automatic code documentation

Figure 1.2 – Automatic code documentation

Most modern IDEs have plugins that support not only documentation but automatic code formatting with go fmt, code linting and autocompletion, debugging, and a language server—a tool that allows developers to navigate through the code by going back and forth between type, variable, and function declarations and their references (gopls, the Go language server: Further reading). This last feature not only allows you to navigate code bases of any complexity without having to resolve import statements manually or search for string patterns in text, but also highlights any type inconsistencies on the fly before you compile a program.

Tooling

When setting up a new environment, one of the first things a typical developer would do is download and install a set of their favorite language tools and libraries to help with testing, formatting, dependency management, and so on. Go comes with all these utilities included by default, which are part of the go command. The following table summarizes some Go built-in tools and their purpose:

Table 1.1 – Go tools

Table 1.1 – Go tools

These are just a few of the most popular tools that get shipped together with the Go binary. This certainly reduces the room for creativity in the tooling ecosystem by giving developers a default choice that is good enough for most average use cases. Another benefit of this artificial scarcity is not having to reinstall and relearn a new set of tools every time you switch between different Go projects.

Go for networking

Some network automation processes can trigger hundreds—if not thousands—of simultaneous connections to network devices. Being able to orchestrate this at scale is one of the things that Go enables us to do.

You can see Egon Elbre's Network Gopher mascot in the following screenshot:

Figure 1.3 – Network Gopher, by Egon Elbre

Figure 1.3 – Network Gopher, by Egon Elbre

Go comes with a strong networking package that offers you all the constructs to create network connections, packages to encode and decode data from popular formats, and primitives to work with bits and bytes.

Concurrency

Go has first-class support for concurrency with the help of lightweight threads managed by the Go runtime, called goroutines. This language construct makes it possible to embed asynchronous functions into an otherwise sequential program.

Any function call that you prepend with the go keyword runs in a separate goroutine—different from the main application goroutine—that does not block execution of the calling program.

Channels are another language feature that allows communication between goroutines. You can think of it as a first-in, first-out (FIFO) queue with sending and receiving ends existing in two different goroutines.

Together, these two powerful language constructs offer a way to write concurrent code in a safe and uniform way that allows you to connect to various networking devices simultaneously, without paying the tax of running an operating system thread for each one. For example, consider the following program in the next code example (https://github.com/PacktPublishing/Network-Automation-with-Go/blob/main/ch01/concurrency/main.go) that simulates interaction with remote network devices:

func main() {
    devices := []string{"leaf01", "leaf02", "spine01"}
    resultCh := make(chan string, len(devices))
    go connect(devices, resultCh)
    fmt.Println("Continuing execution") 
    for msg := range resultCh {
        fmt.Println(msg)
    }
}

Connecting to remote devices can take a long time, and it would normally block the execution of the rest of the program. With the connect function running in a goroutine, as illustrated in the following code snippet, our program can continue its execution, and we can come back and collect the responses at any point in the future:

ch01/concurrency$  go run main.go 
Continuing execution
Connected to device "leaf01"
Connected to device "spine01"
Connected to device "leaf02"

As the remote devices process the requests and return a response, our program starts printing the responses in the order it receives them.

Strong standard library

Go has a versatile standard library that covers different areas that may be applicable to networking—from cryptography to data encoding, from string manipulation to regular expressions (regexes) and templating. Standard library packages such as net and encoding offer interfaces for both client- and server-side network interactions, including the following:

  • Internet Protocol (IP) prefix parsing and comparison functions
  • Client and server implementations for IP, Transmission Control Protocol/User Datagram Protocol (TCP/UDP), and HyperText Transfer Protocol (HTTP) connections
  • Domain Name System (DNS) lookup functions
  • Uniform Resource Locator (URL) parsing and manipulations
  • Serializing data formats such as Extensible Markup Language (XML), binary, and JavaScript Object Notation (JSON) for storage or transmission

Unless you have unique performance requirements, for example, most Go developers recommend against using external libraries for logic that can otherwise be implemented natively with the standard library. All standard packages are thoroughly tested with each release and used extensively in several large-scale projects. All this creates a better learning experience for newcomers because most-often-used data structures and functions are there already.

Data streaming

Network services are I/O-bound in general—they read or write bytes from or to the network. This mode of operation is how data streaming works in Go, which makes it appealing to network engineers who are familiar with byte processing for network protocol parsing, for example.

I/O operations in Go follow a model where a Reader reads data from a source, which can stream as an array of bytes to a Writer that writes that data to a destination. The following diagram should give you a clearer picture of what this means:

Figure 1.4 – Streaming from a network connection to a file example

Figure 1.4 – Streaming from a network connection to a file example

A Reader is an interface that can read from a file, a cipher, a shell command, or a network connection, for example. You can then stream the data you capture to a Writer interface, which could also be a file or most of the other Reader examples.

The Go standard library offers these streaming interfaces, such as net.Conn, that, in this case, allow you to read and write from a network connection, transfer data between interfaces, and transform this data if needed. We cover this topic in much more detail in Chapter 3, Getting Started with Go.

While there are other variables to consider when selecting a programming language to work with, such as which one your company is currently using or which one you feel more comfortable with, our goal is to equip you with all the resources to understand what makes Go so appealing to large-scale system developers. If you want to begin in familiar territory, we compare and contrast Go with Python next. Python is the most popular programming language used for network automation today.

Go versus Python

The topic of comparing programming languages can very quickly turn into a heated debate. We believe all languages have their merits and we don't want to advocate for one being better than the other. Still, we do acknowledge that most people with a network automation background would know and use Python, so it would make sense to present some form of comparison between the two languages and highlight some of their most salient points.

Code execution

One of the biggest differences that affect the developer experience is how you distribute and execute your code.

Python programs require an interpreter to run on a target machine and access to all library dependencies. While there are projects such as Nuitka to compile Python, you need commercial support to obfuscate your source code, for example. Having all source code available allows you to make changes and iterate quickly when developing a feature or troubleshooting a bug.

Go programs do not require an interpreter, as you distribute them as a compiled binary file. Compiling to machine code may seem like an unnecessary hurdle, but compilation takes only a few seconds, and the resulting binary has all its required dependencies, so it's the only file that needs to exist on the target system.

Type system

Go requires all variable types to be statically defined, with type inference allowed only during initial variable declaration.

Although generics are making their way into Go, they do not allow the same amount of freedom as a Python type system. A lack of explicit type declaration makes Python a more approachable language for beginners and for use cases where development speed is more important than code robustness. However, as Python projects become more mature, they must make up for these initial gains by putting more focus on testing.

Performance

Go programs perform better when compared to Python across a wide range of use cases (see The Computer Language Benchmarks Game: Further reading). This is, in part, an outcome of the points we already mentioned in this section, but it's also the result of the effort the Go team has put into optimizing the language.

While things such as goroutines and type definition give Go developers enough tools to write high-performance code, each Go release brings new improvements in memory management and compiler optimizations that make code execution faster in the background.

Ease of use

Python is a language designed to be used for teaching and prototyping. At the same time, it's versatile and powerful enough to write complex programs such as web servers (Flask, Django), machine learning (ML) frameworks (PyTorch, TensorFlow), and infrastructure software (RabbitMQ, Ansible).

As the number of Python projects you work on grows, maintaining different virtual environments for dependency and environment management might become a hassle. This is an area where Go shines, with its self-hosted dependency manager and statically linked binaries.

Despite that, Python continues to hold its dominant position as the most approachable language with a large open source community and is unlikely to relinquish it any time soon.

Memory management

Both languages use dynamic memory management with automatic garbage collection. Most of the time, you wouldn't need to and are not advised to change any of the default settings, although both languages expose a few threshold variables that can be fine-tuned if needed.

The biggest difference comes from the fact that Go allocates memory based on a more precise set of data types and that it does static memory allocation at compile time in the stack for goroutines and functions, and only a subset of variables escape to the heap. In contrast, Python treats everything as an object, so even the most primitive types, such as int or string, are considerably larger, and they are dynamically allocated memory at runtime (in the heap).

Access to memory in the heap is not only slower but also needs to be garbage-collected, which adds an overhead to the program execution.

Syntax

Python has a very lightweight syntax and uses indentation to separate different blocks of code. The lack of trailing semicolons and excessive curly braces make it comprehensible, but writing it without an IDE—which would automatically manage the indentation—can be a challenge.

Go never considered white space for indentation, as the language designers don't believe having your semantics depend on invisible characters is a good idea. This, of course, comes down to personal preferences; formats such as YAML Ain't Markup Language (YAML), for example, also use spaces to structure data.

Go benefits from its built-in formatting tool that auto-indents the code and makes it look neat by automatically inserting blank lines in certain places. Also, Go developers use blank lines to split logically separate a set of lines in a function that makes the final program less dense and easier to read.

Failure handling

Another big difference is in error handling. Python uses implicit error handling as a convention by relying on exceptions that can be carefully caught in parts of code where you expect them to happen. This keeps in line with Python's readability and ease-of-use nature. Go uses explicit error checks, and most functions have errors as the last positional return value. This often results in the code looking like this:

config, err := buildConfig(deviceName)
if err != nil {
   return err
}
 
d, err := connect(deviceName)
if err != nil {
   return err
}
 
if err := configure(d, config); err != nil {
   return err
}

Although this makes a program more robust by forcing the developers to always think about the returned error and act on it as soon as it happens, this does create a lot of visual noise that human brains quickly learn to ignore. This is a recurrent topic in the Go community and one of the areas that Go version 2 is putting a focus on. The Go 2 draft design document for error handling covers the problem and proposal in detail (Further reading).

Concurrency

Concurrency has not only been a feature of Go since day one but also one of the key drivers behind the creation of Go in the first place. Go has enough first-class language constructs to deal with most common concurrency challenges, such as communication between processes and access to shared resources.

By contrast, you cannot run more than two or more Python threads at the same time because the Global Interpreter Lock (GIL) prevents it, which the Python language designers made part of the language early on. This is unless you architect your program to use the threading library. The GIL has performance benefits for single-threaded programs, and removing it from the language has been a recurrent topic in the Python community.

To implement concurrency, Python makes you run multiple processes to leverage all the CPUs that you have at your disposal (multiprocessing or concurrency pools). Over time, different libraries have attempted to improve the performance and user experience (UX) of concurrency in Python, with the most popular one being asyncio.

Despite that, better concurrency and parallelism are in the top three most desired features to add to Python, according to Python Developers Survey 2020 Results (Further reading). Most Python developers don't like the current implementation, as writing concurrent code in Python can be challenging and requires the use of compatible libraries.

Community

Being the more popular language of the two, Python has a larger community with a huge number of open source libraries and frameworks. Although its major use cases are data analysis, web development, and ML (Python Developers Survey 2020 Results: Further reading), today you can find libraries that deal with anything from game development to desktop plugins.

Most importantly, Python is the most popular language for network automation and has amassed many libraries and frameworks to work with network devices. Go has been more systems- and performance-centric, so we don't see as many network libraries and tools. Still, one heavy user of Go in the network engineering community has been the OpenConfig ecosystem, which today includes almost a dozen different projects written in Go.

Go is being rapidly adopted by web-scale companies, which means we are likely to see more network-related projects appearing in the future.

We hope this gives you a perspective and appreciation of the Go language features. The next step is to install Go on your computer.

Installing Go on your computer

The Go download and install instructions (https://golang.org/doc/install#install) require you to download a file from https://go.dev/ and follow a couple of instructions. We include here the steps for Go version 17.7, which is the latest version available at the time of writing. Newer versions of Go 1 should continue to work.

Windows

To install Go on Windows, follow these steps:

  1. Download https://golang.org/dl/go1.17.7.windows-amd64.msi.
  2. Execute the go1.17.7.windows-amd64.msi file and follow the instructions.
  3. Open the Command Prompt window (cmd) and run go version to verify the installation.

Mac

If you have Homebrew installed, you can run brew install go. Otherwise, you can follow these steps:

  1. Download https://golang.org/dl/go1.17.7.darwin-amd64.pkg.
  2. Execute the go1.17.7.darwin-amd64.pkg file and follow the instructions.
  3. Open a Terminal and run go version to verify the installation.

Linux

Go is typically available as a system package in a Linux distribution, but is often an older version. Follow these steps to install a more recent release:

  1. Download https://golang.org/dl/go1.17.7.linux-amd64.tar.gz.
  2. Remove any existing Go installation with rm -rf /usr/local/go.
  3. Extract the archive you downloaded into /usr/local with tar -C /usr/local -xzf go1.17.7.linux-amd64.tar.gz.
  4. Add /usr/local/go/bin to the PATH environment variable with export PATH=$PATH:/usr/local/go/bin. To make this persistent, add this line as well in $HOME/.bash_profile. This last part is valid for bash, but you might want to do something similar if you use a different shell.
  5. Run go version to verify the installation

There you go! You can now download and install Go in your system without any hassle. To install a different version, just replace 17.7 in the instructions with a target version of your choice.

Summary

In this chapter, we reviewed why Go is relevant for networking and network automation. We looked at the various aspects of Go that make it the preferred choice for millions of developers. We also explored how you can install it on your computer. In the next chapter, we will dive deeper into the Go programming language, its source files, and its tools.

Further reading

You can refer to these resources for further reading:

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • A comprehensive guide to the world of modern network automation
  • Use Go to build anything from repetitive task automation to complex distributed systems
  • Over 30 practical, ready-to-use sample programs

Description

Go’s built-in first-class concurrency mechanisms make it an ideal choice for long-lived low-bandwidth I/O operations, which are typical requirements of network automation and network operations applications. This book provides a quick overview of Go and hands-on examples within it to help you become proficient with Go for network automation. It’s a practical guide that will teach you how to automate common network operations and build systems using Go. The first part takes you through a general overview, use cases, strengths, and inherent weaknesses of Go to prepare you for a deeper dive into network automation, which is heavily reliant on understanding this programming language. You’ll explore the common network automation areas and challenges, what language features you can use in each of those areas, and the common software tools and packages. To help deepen your understanding, you’ll also work through real-world network automation problems and apply hands-on solutions to them. By the end of this book, you’ll be well-versed with Go and have a solid grasp on network automation.

Who is this book for?

This book is for all network engineers, administrators, and other network practitioners looking to understand what network automation is and how the Go programming language can help develop network automation solutions. As the first part of the book offers a comprehensive overview of Go’s main features, this book is suitable for beginners with a solid grasp on programming basics.

What you will learn

  • Understand Go programming language basics via network-related examples
  • Find out what features make Go a powerful alternative for network automation
  • Explore network automation goals, benefits, and common use cases
  • Discover how to interact with network devices using a variety of technologies
  • Integrate Go programs into an automation framework
  • Take advantage of the OpenConfig ecosystem with Go
  • Build distributed and scalable systems for network observability

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 05, 2023
Length: 442 pages
Edition : 1st
Language : English
ISBN-13 : 9781800561014
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jan 05, 2023
Length: 442 pages
Edition : 1st
Language : English
ISBN-13 : 9781800561014
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
AU$24.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
AU$249.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts
AU$349.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total AU$ 266.97
Go for DevOps
AU$86.99
Network Automation with Go
AU$110.99
Event-Driven Architecture in Golang
AU$68.99
Total AU$ 266.97 Stars icon

Table of Contents

17 Chapters
Part 1: The Go Programming Language Chevron down icon Chevron up icon
Chapter 1: Introduction Chevron down icon Chevron up icon
Chapter 2: Go Basics Chevron down icon Chevron up icon
Chapter 3: Getting Started with Go Chevron down icon Chevron up icon
Chapter 4: Networking (TCP/IP) with Go Chevron down icon Chevron up icon
Part 2: Common Tools and Frameworks Chevron down icon Chevron up icon
Chapter 5: Network Automation Chevron down icon Chevron up icon
Chapter 6: Configuration Management Chevron down icon Chevron up icon
Chapter 7: Automation Frameworks Chevron down icon Chevron up icon
Part 3: Interacting with APIs Chevron down icon Chevron up icon
Chapter 8: Network APIs Chevron down icon Chevron up icon
Chapter 9: OpenConfig Chevron down icon Chevron up icon
Chapter 10: Network Monitoring Chevron down icon Chevron up icon
Chapter 11: Expert Insights Chevron down icon Chevron up icon
Chapter 12: Appendix : Building a Testing Environment Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(5 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Daniel Eduardo Tudares Castillo Feb 09, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
As someone that, as many other network engineer, started the automation journey with Python, I really appreciated the first chapter, getting to know the Why? and compare Go vs Python gave me a very good perspective at the reasons to look beyond the tools I was already confortable with. From there, I realized that getting started with another language doesn't have to be hard, and this book definitely pave the way for doing just that.This book is very well written and I appreciate the author's effort to cover the topics with examples that you can follow along in your own environment. Finally, I though the Expert Insights chapter was such a great addition and gave a great closure to this book. Getting to know the people in the field and real live use cases is a great motivation and example of how Go is being used out there.Great book, will recommend to anyone in Network Automation, even if you are not interested in learning a new language, there is still a lot of value on understanding what's out there and the options we have.
Amazon Verified review Amazon
M Fernandez Feb 04, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book that goes through fundamental concepts and properly positions Go in the network automation problem space. The hands on excercises are spot on
Amazon Verified review Amazon
Mearaj Bhagad Apr 20, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It's a very good book.It also explains some decent low level stuff in golang.I haven't completed all the chapters, but completed enough to write a review.Highly recommended.Thanks :)
Amazon Verified review Amazon
Christian Adell Feb 27, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you are a network engineer, with some software development foundations, this book will bring your Go knowledge from zero to solving real network automation problems using several network interfaces, and automation frameworks. It's remarkable how Go concepts and packages are introduced progressively, learning by example, and with a lot of references to extend the knowledge.
Amazon Verified review Amazon
Josh Van Deraa Jan 19, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Found the concepts in the book very complete and a good set of examples for working with network devices using GoLang. Definitely the place to start when it comes to interacting with network devices. My current recommendation in this space.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.