Book Image

Network Automation with Go

By : Nicolas Leiva, Michael Kashin
Book Image

Network Automation with Go

By: Nicolas Leiva, Michael Kashin

Overview of this book

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.
Table of Contents (18 chapters)
1
Part 1: The Go Programming Language
6
Part 2: Common Tools and Frameworks
10
Part 3: Interacting with APIs

Control Flow

Control flow constructs are a key building block of any computer program as they allow you to express complex behaviors with conditions and iteration. Go's support for control flow reflects its minimalistic design, which is why you'd mostly see a couple of variations of conditional statements and one version of loop in the entire language specification. It may seem surprising, but this makes Go easier to read, as it forces the same design patterns on all programs. Let's start with the simplest and the most common control flow blocks.

For Loops

In its simplest form, the for loop allows you to iterate over a range of integers while doing some work in each iteration. For example, this is how you would print all numbers from 0 to 4:

func main() {
    for i := 0; i < 5; i++ {
        fmt.Println(i)
    }
}

The first line has the init statement i := 0, the condition statement i < 5 and the post statement i++ separated by semicolons (;). The code continues...