ebook img

Go for DevOps: Learn how to use the Go language to automate servers, the cloud, Kubernetes, GitHub, Packer, and Terraform PDF

634 Pages·2022·9.065 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Go for DevOps: Learn how to use the Go language to automate servers, the cloud, Kubernetes, GitHub, Packer, and Terraform

Go for DevOps Learn how to use the Go language to automate servers, the cloud, Kubernetes, GitHub, Packer, and Terraform John Doak David Justice BIRMINGHAM—MUMBAI Go for DevOps Copyright © 2022 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Group Product Manager: Rahul Nair Publishing Product Manager: Preet Ahuja Senior Editor: Shazeen Iqbal Content Development Editor: Romy Dias Technical Editor: Shruthi Shetty Copy Editor: Safis Editing Project Coordinator: Ashwin Kharwa Proofreader: Safis Editing Indexer: Subalakshmi Govindhan Production Designer: Joshua Misquitta Marketing Coordinator: Sanjana Gupta First published: June 2022 Production reference: 1010622 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80181-889-6 www.packt.com Monika, you are the North Star I guide my ship with and I love you with all my heart. I couldn't have done this without your support and guidance. – John Doak To Deya, Will, Thor, and Tron, without whom my days would be as muted and monotone as this page. – David Justice Contributors About the authors John Doak is the principal manager of Layer 1 Reliability Engineering at Microsoft. John led the development of the Azure Data Explorer and Microsoft Authentication Library Go SDKs. Previously, he was a Staff Site Reliability Engineer at Google. As part of network engineering, he created many of their first network automation systems. John led the migration of that group from Python to Go, developing Go training classes that have been taught around the world. He was a pivotal figure in transforming the network team to a network/systems group that integrated with SRE. Prior to that, he worked for Lucasfilm in video games and film. You can find his musings on Go/SRE topics and his Go classes on the web. I'd like to thank Raleigh Mann who was my manager at both Lucasfilm and Google during most of my time there. His advice and steadfastness when standing beside me are why I still call him Boss today. Stephen Stuart, who gave me my initial foray into management, which I'm not sure I should thank or curse him for. Thanks to Less Lincoln, the man, the myth, the legend. I’ve worked with Less for four years, he’s as much a mystery box as when I first met him (and I think he likes it that way). Of course, I would never have gotten here without the love and support of my parents, I love you both. Thanks to Sarah Murphy, who was an early contributor to the book. And thanks to David Justice. Without his hard work and enthusiasm, I would not have been able to do this. David Justice is the principal software engineer lead for the Azure K8s infrastructure and Steel Thread teams, which maintain a variety of CNCF and Bytecode Alliance projects. He is a maintainer of the Cluster API Provider Azure and a contributor to the Cluster API. Prior to that, David was the technical assistant to the Azure CTO, where he was responsible for Azure cross-group technical strategy and architecture. Early on at Microsoft, he was a program manager leading Azure SDKs and CLIs, where he transitioned all Azure services to describe them using OpenAPI specifications in GitHub and established automations to generate Azure reference docs, SDKs, and CLIs. Prior to working at Microsoft, David was the CTO of a mobile CI/CD SaaS called CISimple. Thank you to my lovely, supportive wife, Deya, for encouraging me to pursue time-consuming activities such as writing a book, educational pursuits, start-ups, and her favorite, golf. Deya and Will, you will never know how much your love, hugs, and support powered me through late nights and droughts of creativity. This book would not be possible without the brilliance, reliability, and counter-perspectives of my coauthor, John, for whom the word example means a 30k-line robust application. Thank you to the whole Packt team and all the reviewers, but especially Romy Dias who edited my work into something more closely resembling English prose. Finally, thank you Dad and Papa for always believing in me. About the reviewers Trieu Pham is a software engineer with various technical ideas. His current focus and specialty are on API development, microservices, DevOps, and Golang. He has a bachelor's degree in mathematics and computer science, a master's degree in computer engineering, and a PhD in engineering. Lukasz Sudol is a senior director of engineering at GumGum. He began working in the e-commerce industry by developing backend systems. During the past decade, his work has focused on developing high-availability application architectures for AdTech. He enjoys helping people to develop and working with interesting technologies. I would like to thank my family, my fiancée Marcelina, and my friends who understand the time and commitment it takes to research and test engineering tools that are constantly changing. And to the reader, I hope my contribution will help you get through the book more easily. Table of Contents Preface Section 1: Getting Up and Running with Go 1 Go Language Basics Technical requirements 5 Using conditionals 24 Using the Go Playground 5 if statements 24 Utilizing Go packages 7 else 25 Declaring a package 7 Learning about functions 28 Importing a package 8 Returning multiple values Using a package 9 and named results 29 Package name conflicts 9 Variadic arguments 30 Packages must be used 10 Anonymous functions 31 A Go Hello World 11 Defining public and Using Go's variable types 12 private 32 Go's types 14 Using arrays and slices 34 Declaring variables 15 Arrays 34 Variable scopes and shadowing 17 Slices 35 Function/statement variable Extracting all values 36 must be used 20 Understanding maps 37 Looping in Go 21 Declaring a map 37 C style 21 Accessing values 38 Removing the init statement 21 Adding new values 38 Remove the post statement Extracting all values 39 too and you have a while loop 22 Understanding Go Creating an infinite loop 22 pointers 39 viii Table of Contents Memory addresses 40 Changing a field's value in Function arguments are a method 47 copies 40 Constructors 48 Pointers to the rescue 41 Comprehending Go Getting to know about interfaces 49 structs 43 Defining an interface type 49 Declaring a struct 43 Important things about Declaring a custom type 44 interfaces 50 Custom struct types 44 The blank interface – Go's Adding methods to a type 45 universal value 51 Changing a field's value 46 Type assertion 51 Summary 52 2 Go Language Essentials Handling errors in Go 54 Channels 69 Creating an error 54 Sending/receiving 70 Using an error 55 select statements 71 Creating named errors 56 Channels as an event signal 72 Custom errors 57 Mutexes 74 Wrapping errors 58 RWMutex 75 Utilizing Go constants 59 Understanding Go's Context type 75 Declaring a constant 59 Enumeration via constants 61 Using a Context to signal a timeout 76 Printing enumerators 62 Honoring a context when Using defer, panic, and receiving 77 recover 63 Context in the standard defer 63 library 78 panic 64 Context to pass values 79 recover 64 Best practices 81 Utilizing goroutines Utilizing Go's testing for concurrency 65 framework 81 Starting a goroutine 66 Creating a basic test file 82 Synchronization 67 Creating a simple test 83 WaitGroups 68 Table Driven Tests (TDT) 84 Table of Contents ix Creating fakes with interfaces 86 Type constraints with Third-party testing packages 90 methods 95 Adding type parameters to struct Generics – the new kid types 96 on the block 91 Specifying the type when Type parameters 91 calling a generic function 98 Using type constraints 92 Gotchas to watch for 100 We could do better with When to use generics 101 constraints 93 Summary 102 Current built-in constraints 94 3 Setting Up Your Environment Technical requirements 103 A note on Go compiler version compatibility 108 Installing Go on your machine 104 Building code locally 108 macOS installation using the Creating a module directory package installer 104 and go.mod file 109 macOS installation via Updating a module when Homebrew 104 adding dependencies 110 Windows installation using Adding a hello world 110 MSI 105 Running our first program 110 Linux 106 Other platforms 108 Summary 111 4 Filesystem Interactions All I/O in Go are files 114 Stdin/Stdout/Stderr are just files 119 I/O interfaces 114 Reading data out of a Reading and writing to stream 120 files 115 Writing data into a stream 122 Reading local files 115 OS-agnostic pathing 123 Writing local files 116 What OS/platform am Reading remote files 117 I running? 124 Streaming file content 119 Using filepath 124

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.