Cover....1
Copyright....3
Contributors....4
Table of Contents....8
Preface....24
Chapter 1: A Quick Introduction to Go....32
Introducing Go....33
The history of Go....33
The advantages of Go....35
When to use Go....36
My personal Go journey....37
The go doc and godoc utilities....38
Hello World!....39
Introducing functions....40
Introducing packages....41
Running Go code....41
Compiling Go code....41
Using Go like a scripting language....42
Important formatting and coding rules....42
What you should know about Go....44
Defining and using variables....44
Constants....45
Global variables....45
Printing variables....45
Controlling program flow....47
Iterating with for loops and range....50
Getting user input....52
Reading from standard input....53
Working with command line arguments....53
Using error variables to differentiate between input types....57
Understanding the Go concurrency model....60
Developing the which(1) utility in Go....62
Logging information....64
log.Fatal() and log.Panic()....66
Writing to a custom log file....68
Printing line numbers in log entries....70
Writing to multiple log files....71
Developing a statistics application....72
Summary....75
Exercises....75
Additional resources....75
Chapter 2: Basic Go Data Types....78
The error data type....79
Numeric data types....83
Avoiding overflows....85
Non-numeric data types....86
Strings, characters, and runes....87
Converting int to string....89
The unicode package....90
The strings package....91
Times and dates....94
Working with different time zones....96
Constants....97
The constant generator iota....97
Typed and untyped constants....100
Grouping similar data....101
Arrays....101
Slices....101
About slice length and capacity....104
Selecting a part of a slice....107
Byte slices....109
Deleting an element from a slice....111
How slices are connected to arrays....114
Catching out of bounds errors....116
The copy() function....117
Sorting slices....119
Pointers....121
Converting a slice to an array or an array pointer....125
Data types and the unsafe package....126
Generating random numbers....128
Generating random strings....130
Generating secure random numbers....131
Updating the statistics application....132
Summary....133
Exercises....134
Additional resources....134
Chapter 3: Composite Data Types....136
Maps....137
How to tell whether a key exists on a map....138
Storing to a nil map....138
Iterating over maps....140
Structures....141
The type keyword....141
Defining new structures....141
Using the new keyword....143
Slices of structures....145
Regular expressions and pattern matching....147
About regexp.Compile and regexp.MustCompile....147
Go regular expressions....149
About raw string and interpreted string literals....150
Matching names and surnames....150
Matching integers....151
Improving the statistics application....152
Working with CSV files....152
The updated version of the statistics application....157
Summary....160
Exercises....161
Additional resources....161
Chapter 4: Go Generics....164
An introduction to generics....165
Hello, generics!....166
Constraints....167
Creating constraints....168
Supporting underlying data types....170
Supporting slices of any type....171
Defining new data types with generics....171
Using generics in Go structures....173
The cmp package....176
The slices package....176
Shallow and deep copies....177
The maps package....179
When to use generics....181
Summary....182
Exercises....182
Additional resources....183
Chapter 5: Reflection and Interfaces....184
Reflection....185
Understanding the internal structure of a Go structure....186
Changing structure values using reflection....189
The three disadvantages of reflection....190
Type methods....191
Creating type methods....191
Value and point receivers....192
Using type methods....193
Interfaces....196
The sort.Interface interface....199
The empty interface....201
Type assertions and type switches....203
The map[string]interface{} map....208
The error data type....212
Writing your own interfaces....215
Using a Go interface....216
Object-oriented programming in Go....217
Interfaces versus generics....221
Reflection versus generics....224
Updating the statistics application....226
Summary....229
Exercises....230
Additional resources....230
Chapter 6: Go Packages and Functions....232
Go packages....233
About go get and go install....234
Functions....237
Anonymous functions....237
Functions that return multiple values....238
The return values of a function can be named....239
Functions that accept other functions as parameters....240
Functions can return other functions....242
Variadic functions....243
The defer keyword....247
Big O complexity....250
Developing your own packages....250
The init() function....251
Order of execution....252
Using GitHub to store Go packages....254
A package for working with SQLite....255
Working with SQLite3 and Go....256
Storing the Go package....265
The design of the Go package....265
The implementation of the Go package....266
Testing the Go package....275
Modules....281
Creating better packages....281
Generating documentation....283
Workspaces....288
Versioning utilities....291
Summary....293
Exercises....294
Additional resources....294
Chapter 7: Telling a UNIX System What to Do....296
stdin, stdout, and stderr....297
UNIX processes....297
File I/O....298
The io.Reader and io.Writer interfaces....298
Using and misusing io.Reader and io.Writer....299
Buffered and unbuffered file I/O....303
Reading text files....304
Reading a text file line by line....304
Reading a text file word by word....306
Reading a text file character by character....307
Reading from /dev/random....309
Reading a specific amount of data from a file....310
Writing to a file....311
Working with JSON....314
Using Marshal() and Unmarshal()....314
Structures and JSON....316
Reading and writing JSON data as streams....318
Pretty printing JSON records....319
The viper package....321
Using command line flags....321
Reading JSON configuration files....324
The cobra package....328
A utility with three commands....330
Adding command line flags....330
Creating command aliases....331
Creating subcommands....332
Important Go features....333
Embedding files....333
ReadDir and DirEntry....336
The io/fs package....338
Updating the statistics application....340
The slog package....341
Sending logs to io.Discard....343
Using cobra....343
Storing and loading JSON data....345
Implementing the list command....346
Implementing the insert command....347
Summary....348
Exercises....349
Additional resources....349
Chapter 8: Go Concurrency....352
Processes, threads, and goroutines....353
The Go scheduler....354
The GOMAXPROCS environment variable....356
Concurrency and parallelism....357
Goroutines....358
Creating a goroutine....358
Creating multiple goroutines....359
Waiting for all goroutines to finish....360
What if the number of Add() and Done() calls differ?....362
Creating multiple files with goroutines....364
Channels....365
Writing to and reading from a channel....365
Receiving from a closed channel....368
Channels as function parameters....369
Race conditions are bad....370
The Go race detector....371
The select keyword....373
Timing out a goroutine....375
Timing out a goroutine inside main()....375
Timing out a goroutine outside main()....377
Go channels revisited....378
Buffered channels....379
nil channels....381
Worker pools....382
Signal channels....386
Specifying the order of execution for your goroutines....386
Handling UNIX signals....389
Handling two signals....392
Shared memory and shared variables....392
The sync.Mutex type....392
What happens if you forget to unlock a mutex?....394
The sync.RWMutex type....395
The atomic package....398
Sharing memory using goroutines....400
Closured variables and the go statement....403
The context package....405
About context.WithCancelCause....409
The semaphore package....410
Making the statistics application concurrent....413
Summary....416
Exercises....416
Additional resources....417
Chapter 9: Building Web Services....418
The net/http package....418
The http.Response type....419
The http.Request type....419
The http.Transport type....420
Creating a web server....421
Updating the statistics application....425
Defining the API....425
Implementing the handlers....426
Creating a Docker image....435
Developing web clients....437
Using http.NewRequest() to improve the client....439
Using errGroup....442
Creating a client for the statistics service....444
Timing out HTTP connections....450
Using SetDeadline()....450
Setting the timeout period on the client side....452
Setting the timeout period on the server side....454
Summary....455
Exercises....455
Additional resources....455
Chapter 10: Working with TCP/IP and WebSocket....458
TCP/IP....459
The nc(1) command line utility....460
The net package....460
Developing a TCP client....461
Developing a TCP client with net.Dial()....461
Developing a TCP client that uses net.DialTCP()....463
Developing a TCP server....465
Developing a TCP server with net.Listen()....465
Developing a TCP server that uses net.ListenTCP()....468
Developing a UDP client....470
Developing a UDP server....472
Developing concurrent TCP servers....475
Creating a WebSocket server....478
The implementation of the server....479
Using websocat....482
Creating a WebSocket client....484
Working with RabbitMQ....488
Running RabbitMQ....489
Writing to RabbitMQ....490
Reading from RabbitMQ....492
How to remove a module....494
Summary....494
Exercises....495
Additional resources....495
Chapter 11: Working with REST APIs....498
An introduction to REST....499
Developing RESTful servers and clients....501
A RESTful server....502
A RESTful client....511
Creating a functional RESTful server....521
The REST API....521
Using gorilla/mux....522
The use of subrouters....523
The Gin HTTP framework....524
Gin versus Gorilla....524
Working with the database....524
Implementing the RESTful server....529
Testing the RESTful server....534
Testing GET handlers....535
Testing POST handlers....536
Testing the PUT handler....537
Testing the DELETE handler....538
Creating a RESTful client....538
Creating the structure of the command line client....539
Implementing the RESTful client commands....540
Using the RESTful client....545
Working with multiple REST API versions....546
Summary....547
Exercises....547
Additional resources....548
Chapter 12: Code Testing and Profiling....550
Optimizing code....551
Rewriting the main() function for better testing....552
Profiling code....553
Profiling a command line application....554
Profiling an HTTP server....558
The web interface of the Go profiler....560
The go tool trace utility....562
Tracing a web server from a client....564
Visiting all routes of a web server....567
Testing Go code....571
Writing tests for ./ch03/intRE.go....572
Testing UNIX signals....574
Disabling test caching....577
The testing.TempDir() function....577
The Cleanup() function....577
The testing/quick package....580
Timing out tests....582
Testing using testing.T.Fatal() and testing.T.Fatalf()....583
Table-driven tests....585
Testing code coverage....587
Finding unreachable Go code....590
Testing an HTTP server with a database backend....593
The govulncheck tool....599
Installing the tool....599
Cross-compilation....603
Using go:generate....604
Creating example functions....607
Summary....608
Exercises....609
Additional resources....609
Chapter 13: Fuzz Testing and Observability....612
Fuzz testing....613
A simple fuzz testing example....614
An advanced fuzz testing example....617
Correcting the bug....623
Observability....629
The runtime/metrics package....630
Measuring the execution time of a function....632
The expvar package....634
Learning about CPU characteristics....636
Exposing metrics to Prometheus....638
Exposing metrics....638
Creating a Docker image for a Go server....642
Specifying the metrics to expose....644
Getting the metrics....647
Putting the metrics in Prometheus....648
Visualizing Prometheus metrics in Grafana....653
Summary....655
Exercises....655
Additional resources....655
Chapter 14: Efficiency and Performance....658
Benchmarking code....659
A simple benchmark scenario....660
Benchmarking the number of memory allocations....663
Initial version....663
Improving the number of memory allocations....664
Buffered versus unbuffered file I/O....668
The benchstat utility....672
Wrongly defined benchmark functions....674
Go memory management....675
Heap and stack....675
The main elements of the Go memory model....679
Memory leaks....679
Slices and memory leaks....680
Maps and memory leaks....682
Memory pre-allocation....683
Working with eBPF....685
What is eBPF?....685
About observability and eBPF....686
Creating an eBPF tool in Go....686
Summary....690
Exercises....690
Additional resources....690
Chapter 15: Changes in Recent Go Versions....692
About rand.Seed()....692
What is new in Go 1.21?....693
The sync.OnceFunc() function....693
The clear function....695
What is new in Go 1.22?....696
Changes in slices....697
Changes in for loops....698
The math/rand/v2 package....699
Summary....701
Exercises....701
Additional resources....701
Appendix: The Go Garbage Collector....704
Garbage collection....704
The key characteristics of the Go GC....705
Learning more about the Go GC....705
The tri-color algorithm....708
More about the operation of the Go GC....711
Maps, slices, and the Go GC....712
Using slices....712
Using maps with pointers....713
Using maps without pointers....714
Splitting a map....714
Comparing the performance of the presented techniques....715
Additional resources....716
Packt Page....718
Other Books You May Enjoy....720
Index....724
Dive into the core of Go programming and cover advanced topics like generics, concurrency, web services, and cutting-edge testing techniques in this comprehensive fourth edition.
Get With Your Book: PDF Copy, AI Assistant, and Next-Gen Reader Free
Mastering Go, now in its fourth edition, remains the go-to resource for real-world Go development. This comprehensive guide delves into advanced Go concepts, including RESTful servers, and Go memory management. This edition brings new chapters on Go Generics and fuzzy Testing, and an enriched exploration of efficiency and performance. As you work your way through the chapters, you will gain confidence and a deep understanding of advanced Go topics, including concurrency and the operation of the Garbage Collector, using Go with Docker, writing powerful command-line utilities, working with JavaScript Object Notation (JSON) data, and interacting with databases.
You will be engaged in real-world exercises, build network servers, and develop robust command-line utilities. With in-depth chapters on RESTful services, the WebSocket protocol, and Go internals, you are going to master Go's nuances, optimization, and observability. You will also elevate your skills in efficiency, performance, and advanced testing.
With the help of Mastering Go, you will become an expert Go programmer by building Go systems and implementing advanced Go techniques in your projects.
Mastering Go is written primarily for Go programmers who have some experience with the language and want to become expert practitioners. You will need to know the basics of computer programming before you get started with this book, but beyond that, anyone can sink their teeth into it.