Title Page....3
Copyright Page....4
Table of Contents....7
Introduction....13
About This Book....13
Foolish Assumptions....14
Icons Used in This Book....14
Beyond the Book....15
Where to Go from Here....15
Part 1 Getting Started with Go....17
Chapter 1 Hello, Go!....19
Seeing What Learning Go Can Do for You....20
Installing Go on Your Machine....21
macOS....22
Windows....23
Using an Integrated Development Environment with Go....24
Writing Your First Go Program....26
Compiling and running the program....27
Understanding how a Go program works....29
Making sense of the Go file structure....30
Compiling for multiple operating systems....31
Comparing Go with Other Languages....33
Syntax....33
Compilation....34
Concurrency....34
Library support....34
Chapter 2 Working with Different Data Types....35
Declaring Always-Changing Variables....36
Using the var keyword: Type-inferred variables....36
Specifying the data type: Explicitly typed variables....37
Using the short variable declaration operator....38
Declaring Never-Changing Constants....39
Removing Unused Variables....39
Dealing with Strings....41
Performing Type Conversions....42
Discovering the type of a variable....43
Converting a variable’s type....44
Interpolating strings....46
Chapter 3 Making Decisions....49
Using If/Else Statements to Make Decisions....49
Laying the foundation for the if/else statement: Logical and comparison operators....50
Using the if/else statement....52
Short-circuiting: Evaluating conditions in Go....54
When You Have Too Many Conditions: Using the Switch Statement....58
Switching with fall-throughs....59
Matching multiple cases....60
Switching without condition....60
Chapter 4 Over and Over and Over: Using Loops....63
Performing Loops Using the for Statement....63
Iterating over a Range of Values....68
Iterating through arrays/slices....68
Iterating through a string....70
Using Labels with the for Loop....71
Chapter 5 Grouping Code into Functions....77
Defining a Function....77
Defining functions with parameters....78
Defining functions with multiple parameters....80
Passing arguments by value and by pointer....80
Returning values from functions....83
Naming return values....84
Working with variadic functions....84
Using Anonymous Functions....85
Declaring an anonymous function....85
Implementing closure using anonymous functions....86
Implementing the filter() function using closure....88
Part 2 Working with Data Structures....91
Chapter 6 Slicing and Dicing Using Arrays and Slices....93
Arming Yourself to Use Arrays....93
Declaring an array....94
Initializing an array....95
Working with multidimensional arrays....95
Sleuthing Out the Secrets of Slices....98
Creating an empty slice....98
Creating and initializing a slice....100
Appending to a slice....100
Slicing and Ranging....104
Extracting part of an array or slice....104
Iterating through a slice....107
Making copies of an array or slice....107
Inserting an item into a slice....109
Removing an item from a slice....111
Chapter 7 Defining the Blueprints of Your Data Using Structs....113
Defining Structs for a Collection of Items....113
Creating a Go Struct....116
Making a Copy of a Struct....117
Defining Methods in Structs....119
Comparing Structs....122
Chapter 8 Establishing Relationships Using Maps....125
Creating Maps in Go....125
Initializing a map with a map literal....127
Checking the existence of a key....127
Deleting a key....128
Getting the number of items in a map....128
Iterating over a map....129
Getting all the keys in a map....129
Setting the iteration order in a map....130
Sorting the items in a map by values....130
Using Structs and Maps in Go....133
Creating a map of structs....133
Sorting a map of structs....136
Chapter 9 Encoding and Decoding Data Using JSON....141
Getting Acquainted with JSON....141
Object....142
String....142
Boolean....143
Number....143
Object....144
Array....144
null....145
Decoding JSON....146
Decoding JSON to a struct....147
Decoding JSON to arrays....148
Decoding embedded objects....149
Mapping custom attribute names....152
Mapping unstructured data....153
Encoding JSON....156
Encoding structs to JSON....156
Encoding interfaces to JSON....160
Chapter 10 Defining Method Signatures Using Interfaces....163
Working with Interfaces in Go....164
Defining an interface....164
Implementing an interface....165
Looking at How You May Use Interfaces....166
Adding methods to a type that doesn’t satisfy an interface....170
Using the Stringer interface....171
Implementing multiple interfaces....172
Using an empty interface....173
Determining whether a value implements a specific interface....174
Part 3 Multitasking in Go....175
Chapter 11 Threading Using Goroutines....177
Understanding Goroutines....178
Using Goroutines with Shared Resources....180
Seeing how shared resources impact goroutines....180
Accessing shared resources using mutual exclusion....183
Using atomic counters for modifying shared resources....184
Synchronizing Goroutines....186
Chapter 12 Communicating between Goroutines Using Channels....191
Understanding Channels....191
How channels work....192
How channels are used....195
Iterating through Channels....198
Asynchronously Waiting on Channels....199
Using Buffered Channels....204
Part 4 Organizing Your Code....207
Chapter 13 Using and Creating Packages in Go....209
Working with Packages....209
Creating shareable packages....212
Organizing packages using directories....214
Using Third-Party Packages....216
Emojis for Go....216
Go Documentation....217
Chapter 14 Grouping Packages into Modules....223
Creating a Module....223
Testing and Building a Module....226
Publishing a Module on GitHub....228
Part 5 Seeing Go in Action....235
Chapter 15 Consuming Web APIs Using Go....237
Understanding Web APIs....237
Fetching Data from Web Services in Go....238
Writing a Go program to connect to a web API....239
Decoding JSON data....241
Refactoring the code for decoding JSON data....245
Fetching from multiple web services at the same time....250
Returning Goroutine’s results to the main() function....251
Chapter 16 Getting Ready to Serve Using REST APIs....255
Building Web Services Using REST APIs....255
HTTP messages....256
REST URLs....256
REST methods....258
REST response....260
Creating a REST API in Go....261
Getting your REST API up and running....261
Testing the REST API....263
Registering additional paths....263
Passing in query string....266
Specifying request methods....267
Storing the course information on the REST API....269
Testing the REST API again....279
Chapter 17 Working with Databases....283
Setting Up a MySQL Database Server....284
Interfacing with the MySQL server....284
Creating a database and table....286
Creating a new account and granting permission....287
Connecting to the MySQL Database in Go....288
Retrieving a record....290
Adding a record....292
Modifying a record....293
Deleting a record....295
Part 6 The Part of Tens....297
Chapter 18 Ten Useful Go Packages to Create Applications....299
color....299
Installation....300
Code sample....300
now....300
Installation....300
Code sample....300
go-pushbullet....301
Installation....301
Code sample....301
goid....302
Installation....302
Code sample....302
json2go....303
Installation....303
Code sample....303
gojq....304
Installation....305
Code sample....305
turtle....306
Installation....306
Code sample....306
go-http-client....307
Installation....307
Code sample....307
notify....308
Installation....308
Code sample....308
gosx-notifier....309
Installation....309
Code sample....309
Chapter 19 Ten Great Go Resources....311
The Official Go Website....311
Go by Example....312
A Tour of Go....312
The Go Frequently Asked Questions....312
The Go Playground....312
Go Bootcamp....313
Effective Go....313
Gophercises....313
Tutorialspoint....313
Stack Overflow....314
Index....315
EULA....339
Now is the perfect time to learn the Go Programming Language. It’s one of the most in-demand languages among tech recruiters and developers love its simplicity and power. Go Programming Language For Dummies is an easy way to add this top job skill to your toolkit. Written for novice and experienced coders alike, this book traverses basic syntax, writing functions, organizing data, building packages, and interfacing with APIs.
Go―or GoLang, as it’s also known―has proven to be a strong choice for developers creating applications for the cloud-based world we live in. This book will put you on the path to using the language that’s created some of today’s leading web applications, so you can steer your career where you want to Go!
With this book, you can join the growing numbers of developers using Go to create 21st century solutions. Step inside to take start writing code that puts data in users’ hands.