Introduction 1 About This Book 1 Foolish Assumptions 2 Icons Used in This Book 2 Beyond the Book 3 Where to Go from Here 3 Part 1: Getting Started with Go 5 Chapter 1: Hello, Go! 7 Seeing What Learning Go Can Do for You 8 Installing Go on Your Machine 9 macOS 10 Windows 11 Using an Integrated Development Environment with Go 12 Writing Your First Go Program 14 Compiling and running the program 15 Understanding how a Go program works 17 Making sense of the Go file structure 18 Compiling for multiple operating systems 19 Comparing Go with Other Languages 21 Syntax 21 Compilation 22 Concurrency 22 Library support 22 Chapter 2: Working with Different Data Types 23 Declaring Always-Changing Variables 24 Using the var keyword: Type-inferred variables 24 Specifying the data type: Explicitly typed variables 25 Using the short variable declaration operator 26 Declaring Never-Changing Constants 27 Removing Unused Variables 27 Dealing with Strings 29 Performing Type Conversions 30 Discovering the type of a variable 31 Converting a variable''s type 32 Interpolating strings 34 Chapter 3: Making Decisions 37 Using If/Else Statements to Make Decisions 37 Laying the foundation for the if/else statement: Logical and comparison operators 38 Using the if/else statement 40 Short-circuiting: Evaluating conditions in Go 42 When You Have Too Many Conditions: Using the Switch Statement 46 Switching with fall-throughs 47 Matching multiple cases 48 Switching without condition 48 Chapter 4: Over and Over and Over: Using Loops 51 Performing Loops Using the for Statement 51 Iterating over a Range of Values 56 Iterating through arrays/slices 56 Iterating through a string 58 Using Labels with the for Loop 59 Chapter 5: Grouping Code into Functions 65 Defining a Function 65 Defining functions with parameters 66 Defining functions with multiple parameters 68 Passing arguments by value and by pointer 68 Returning values from functions 71 Naming return values 72 Working with variadic functions 72 Using Anonymous Functions 73 Declaring an anonymous function 73 Implementing closure using anonymous functions 74 Implementing the filter() function using closure 76 Part 2: Working with Data Structures 79 Chapter 6: Slicing and Dicing Using Arrays and Slices 81 Arming Yourself to Use Arrays 81 Declaring an array 82 Initializing an array 83 Working with multidimensional arrays 83 Sleuthing Out the Secrets of Slices 86 Creating an empty slice 86 Creating and initializing a slice 88 Appending to a slice 88 Slicing and Ranging 92 Extracting part of an array or slice 92 Iterating through a slice 95 Making copies of an array or slice 95 Inserting an item into a slice 97 Removing an item from a slice 99 Chapter 7: Defining the Blueprints of Your Data Using Structs 101 Defining Structs for a Collection of Items 101 Creating a Go Struct 104 Making a Copy of a Struct 105 Defining Methods in Structs 107 Comparing Structs 110 Chapter 8: Establishing Relationships Using Maps 113 Creating Maps in Go 113 Initializing a map with a map literal 115 Checking the existence of a key 115 Deleting a key 116 Getting the number of items in a map 116 Iterating over a map 117 Getting all the keys in a map 117 Setting the iteration order in a map 118 Sorting the items in a map by values 118 Using Structs and Maps in Go 121 Creating a map of structs 121 Sorting a map of structs 124 Chapter 9: Encoding and Decoding Data Using JSON 129 Getting Acquainted with JSON 129 Object 130 String 130 Boolean 131 Number 131 Object 132 Array 132 null 133 Decoding JSON 134 Decoding JSON to a struct 135 Decoding JSON to arrays 136 Decoding embedded objects 137 Mapping custom attribute names 140 Mapping unstructured data 141 Encoding JSON 144 Encoding structs to JSON 144 Encoding interfaces to JSON 148 Chapter 10: Defining Method Signatures Using Interfaces 151 Working with Interfaces in Go 152 Defining an interface 152 Implementing an interface 153 Looking at How You May Use Interfaces 154 Adding methods to a type that doesn''t satisfy an interface 158 Using the Stringer interface 159 Implementing multiple interfaces 160 Using an empty interface 161 Determining whether a value implements a specific interface 162 Part 3: Multitasking in Go 163 Chapter 11: Threading Using Goroutines 165 Understanding Goroutines 166 Using Goroutines with Shared Resources 168 Seeing how shared resources impact goroutines 168 Accessing shared resources using mutual exclusion 171 Using atomic counters for modifying shared resources 172 Synchronizing Goroutines 174 Chapter 12: Communicating between Goroutines Using Channels 179 Understanding Channels 179 How channels work 180 How channels are used 183 Iterating through Channels 186 Asynchronously Waiting on Channels 187 Using Buffered Channels 192 Part 4: Organizing Your Code 195 Chapter 13: Using and Creating Packages in Go 197 Working with Packages 197 Creating shareable packages 200 Organizing packages using directories 202 Using Third-Party Packages 204 Emojis for Go 204 Go Documentation 205 Chapter 14: Grouping Packages into Modules 211 Creating a Module 211 Testing and Building a Module 214 Publishing a Module on GitHub 216 Part 5: Seeing Go in Action 223 Chapter 15: Consuming Web APIs Using Go 225 Understanding Web APIs 225 Fetching Data from Web Services in Go 226 Writing a Go program to connect to a web API 227 Decoding JSON data 229 Refactoring the code for decoding JSON data 233 Fetching from multiple web services at the same time 238 Returning Goroutine''s results to the main() function 239 Chapter 16: Getting Ready to Serve Using REST APIs 243 Building Web Services Using REST APIs 243 HTTP messages 244 REST URLs 244 REST methods 246 REST response 248 Creating a REST API in Go 249 Getting your REST API up and running 249 Testing the REST API 251 Registering additional paths 251 Passing in query string 254 Specifying request methods 255 Storing the course information on the REST API 257 Testing the REST API again 267 Chapter 17: Working with Databases 271 Setting Up a MySQL Database Server 272 Interfacing with the MySQL server 272 Creating a database and table 274 Creating a new account and granting permission 275 Connecting to the MySQL Database in Go 276 Retrieving a record 278 Adding a record 280 Modifying a record 281 Deleting a record 283 Part 6: The Part of Tens 285 Chapter 18: Ten Useful Go Packages to Create Applications 287 color 287 Installation 288 Code sample 288 now 288 Installation 288 Code sample 288 go-pushbullet 289 Installation 289 Code sample 289 goid 290 Installation 290 Code sample 290 json2go 291 Installation 291 Code sample 291 gojq 292 Installation 293 Code sample 293 turtle 294 Installation 294 Code sample 294 go-http-client 295 Installation 295 Code sample 295 notify 296 Installation 296 Code sample 296 gosx-notifier 297 Installation 297 Code sample 297 Chapter 19: Ten Great Go Resources 299 The Official Go Website 299 Go by Example 300 A Tour of Go 300 The Go Frequently Asked Questions 300 The Go Playground 300 Go Bootcamp 301 Effective Go 301 Gophercises 301 Tutorialspoint 301 Stack Overflow 302 Index 303.
Go Programming Language for Dummies