Create Golang production applications using network libraries, concurrency, and advanced Go data structures
Mihalis Tsoukalos
Book Details
Price
|
4.00 |
---|---|
Pages
| 598 p |
File Size
|
8,289 KB |
File Type
|
PDF format |
ISBN
| 978-1-78862-654-5 |
Copyright©
| 2018 Packt Publishing |
Mihalis Tsoukalos is a technical author, a Unix administrator, a developer, and a
mathematician, who enjoys learning new things. He has written more than 250 technical
articles for many publications, including Sys Admin, MacTech, Linux User and Developer,
Usenix ;login:, Linux Format, and Linux Journal.
Mihalis is also the author of Go Systems Programming, by Packt Publishing, 2017 and the
technical editor for MongoDB in Action, Second Edition, by Manning. Mihalis' research
interests include databases, operating systems, and statistics. You can reach him at http:/ /
www.mtsoukalos.eu/ and @mactsouk. He is also a photographer (http://www.highiso.net/ ).
I would like to thank the people at Packt Publishing for helping me write this book,
including Frank Pohlmann and Gary Schwartz, my technical reviewer, Mat Ryer, Radhika
Atitkar, for her encouragement and trust, and Kishor Rit, for answering all my questions
and encouraging me during the whole process.
For all people everywhere: You will never change your life until you change something you do daily!
About the reviewer
Mat Ryer has been programming computers since he was 6 years old. He would build
games and programs, first in BASIC on a ZX Spectrum and then in AmigaBASIC and
AMOS on Commodore Amiga with his father. Many hours were spent on manually
copying the code from the Amiga Format magazine and tweaking variables or moving
GOTO statements around to see what might happen. The same spirit of exploration and
obsession with programming led Mat to starting work with a local agency in Mansfield,
England, when he was 18, where he started to build websites and other online services.
After several years of working with various technologies and industries in London and
around the world, Mat noticed a new systems language called Go that Google was
pioneering. Since it addressed very pertinent and relevant modern technical challenges, Mat
started using it to solve problems while the language was still in the beta stage. He has used
it ever since. Mat contributes to open-source projects and founded Go packages, including
Testify, Moq, Silk, and Is, as well as a macOS developer tool called BitBar.
In 2018, Mat co-founded Machine Box and still spends a lot of time speaking at conferences,
writing about Go on his blog, and is an active member of the Go community.
Preface
The book you are reading right now is called Mastering Go and is all about helping you
become a better Go developer!
I tried to include the right amount of theory and hands on practice, but only you, the reader,
can tell if I succeeded or not! Additionally, all presented examples are self-contained, which
means that they can be used on their own or as templates for creating more complex
applications.
Please try to do the exercises located at the end of each chapter and do not hesitate to
contact me with ways to make any future editions of this book even better!
Table of Contents
Preface 1
Chapter 1: Go and the Operating System 7
The structure of the book 8
The history of Go 8
Why learn Go? 9
Go advantages 9
Is Go perfect? 11
What is a preprocessor? 11
The godoc utility 12
Compiling Go code 13
Executing Go code 14
Two Go rules 14
You either use a Go package or do not include it 15
There is only one way to format curly braces 16
Downloading Go packages 17
Unix stdin, stdout, and stderr 19
About printing output 19
Using standard output 21
Getting user input 23
About := and = 23
Reading from standard input 24
Working with command-line arguments 26
About error output 28
Writing to log files 30
Logging levels 31
Logging facilities 31
Log servers 31
A Go program that sends information to log files 32
About log.Fatal() 35
About log.Panic() 36
Error handling in Go 38
The error data type 38
Error handling 40
Additional resources 43
Exercises 44
Summary 44
Chapter 2: Understanding Go Internals 45
The Go compiler 46
Garbage Collection 47
The Tricolor algorithm 50
More about the operation of the Go Garbage Collector 53
Unsafe code 55
About the unsafe package 57
Another example of the unsafe package 57
Calling C code from Go 59
Calling C code from Go using the same file 59
Calling C code from Go using separate files 60
The C code 61
The Go code 62
Mixing Go and C code 63
Calling Go functions from C code 64
The Go package 64
The C code 66
The defer keyword 67
Panic and Recover 69
Using the panic function on its own 71
Two handy Unix utilities 72
The strace tool 73
The dtrace tool 74
Your Go environment 76
The Go Assembler 78
Node Trees 79
Learning more about go build 85
General Go coding advices 86
Additional Resources 86
Exercises 87
Summary 87
Chapter 3: Working with Basic Go Data Types 89
Go loops 90
The for loop 90
The while loop 91
The range keyword 91
Examples of Go for loops 91
Go arrays 93
Multi-dimensional arrays 94
The shortcomings of Go arrays 97
Go slices 97
Performing basic operations on slices 98
Slices are being expanded automatically 100
Byte slices 102
The copy() function 102
Multidimensional slices 105
Another example of slices 105
Sorting slices using sort.slice() 108
Go maps 110
Storing to a nil map 112
When you should use a map? 113
Go constants 113
The constant generator iota 115
Go pointers 118
Dealing with times and dates 121
Working with times 123
Parsing times 123
Working with dates 125
Parsing dates 125
Changing date and time formats 127
Additional resources 129
Exercises 129
Summary 129
Chapter 4: The Uses of Composite Types 130
About composite types 131
Structures 131
Pointers to structures 134
Using the new keyword 136
Tuples 136
Regular expressions and pattern matching 138
Now for some theory 139
A simple example 139
A more advanced example 142
Matching IPv4 addresses 145
Strings 150
What is a rune? 153
The Unicode package 155
The strings package 156
The switch statement 160
Calculating Pi with great accuracy 164
Developing a key/value store in Go 167
Additional resources 172
Exercises 173
Summary 173
Chapter 5: Enhancing Go Code with Data Structures 174
About graphs and nodes 175
Algorithm complexity 175
Binary trees in Go 176
Implementing a binary tree in Go 177
Advantages of binary trees 179
Hash tables in Go 180
Implementing a hash table in Go 181
Implementing the lookup functionality 184
Advantages of hash tables 185
Linked lists in Go 185
Implementing a linked list in Go 186
Advantages of linked lists 190
Doubly linked lists in Go 190
Implementing a doubly linked list in Go 192
Advantages of doubly linked lists 195
Queues in Go 195
Implementing a queue in Go 196
Stacks in Go 199
Implementing a stack in Go 199
The container package 202
Using container/heap 203
Using container/list 206
Using container/ring 208
Generating random numbers 210
Generating random strings 213
Additional Resources 216
Exercises 216
Summary 217
Chapter 6: What You Might Not Know About Go Packages 218
About Go packages 219
About Go functions 219
Anonymous functions 220
Functions that return multiple values 220
The return values of a function can be named! 222
Functions with pointer parameters 224
Functions that return pointers 225
Functions that return other functions 227
Functions that accept other functions as parameters 228
Developing your own Go packages 230
Compiling a Go package 232
Private variables and functions 232
The init() function 232
Reading the Go code of a standard Go package 235
Exploring the code of the net/url package 235
Looking at the Go code of the log/syslog package 237
Creating good Go packages 238
The syscall package 240
Finding out how fmt.Println() really works 243
Text and HTML templates 245
Generating text output 246
Constructing HTML output 248
Basic SQLite3 commands 256
Additional resources 256
Exercises 257
Summary 257
Chapter 7: Reflection and Interfaces for All Seasons 258
Type methods 258
Go interfaces 261
About type assertion 262
Developing your own interfaces 264
Using a Go interface 265
Using switch with interface and data types 267
Reflection 269
A simple Reflection example 270
A more advanced reflection example 272
The three disadvantages of reflection 275
Object-oriented programming in Go! 276
Additional resources 280
Exercises 280
Summary 281
Chapter 8: Telling a Unix System What to Do 282
About Unix processes 283
The flag package 283
The io.Reader and io.Writer interfaces 289
Buffered and unbuffered file input and output 289
The bufio package 289
Reading text files 290
Reading a text file line by line 290
Reading a text file word by word 292
Reading a text file character by character 294
Reading from /dev/random 296
Reading the amount of data you want from a file 298
Why are we using binary format? 300
Reading CSV files 301
Writing to a file 304
Loading and saving data on disk 307
The strings package revisited 311
About the bytes package 313
File permissions 315
Handling Unix signals 316
Handling two signals 317
Handling all signals 319
Programming Unix pipes in Go 322
Implementing the cat(1) utility in Go 322
Traversing directory trees 324
Using eBPF from Go 327
About syscall.PtraceRegs 328
Tracing system calls 330
User ID and group ID 335
Additional resources 336
Exercises 337
Summary 338
Chapter 9: Go Concurrency – Goroutines, Channels, and Pipelines 339
About processes, threads, and goroutines 340
The Go scheduler 341
Concurrency and parallelism 341
Goroutines 342
Creating a goroutine 342
Creating multiple goroutines 344
Waiting for your goroutines to finish 346
What if the number of Add() and Done() calls do not agree? 348
Channels 350
Writing to a channel 350
Reading from a channel 352
Channels as function parameters 354
Pipelines 355
Additional resources 359
Exercises 359
Summary 360
Chapter 10: Go Concurrency – Advanced Topics 361
The Go scheduler revisited 362
The GOMAXPROCS environment variable 364
The select keyword 365
Timing out a goroutine 368
Timing out a goroutine – take 1 368
Timing out a goroutine – take 2 370
Go channels revisited 373
Signal channels 374
Buffered channels 374
Nil channels 377
Channel of channels 378
Specifying the order of execution for your goroutines 381
Shared memory and shared variables 384
The sync.Mutex type 385
What happens if you forget to unlock a mutex? 387
The sync.RWMutex type 389
Sharing memory using goroutines 393
Catching race conditions 395
The context package 401
An advanced example of the context package 405
Worker pools 410
Additional resources 415
Exercises 416
Summary 417
Chapter 11: Code Testing, Optimization, and Profiling 418
The Go version used in this chapter 419
Comparing Go version 1.10 with Go version 1.9 419
Installing a beta or RC version of Go 420
About optimization 422
Optimizing Go code 422
Profiling Go code 423
The net/http/pprof standard Go package 424
A simple profiling example 424
A convenient external package for profiling 432
The web interface of the Go profiler 434
A profiling example that uses the web interface 434
A quick introduction to Graphviz 437
The go tool trace utility 438
Testing Go code 444
Writing tests for existing Go code 445
Benchmarking Go code 449
A simple benchmarking example 449
A wrong benchmark function 455
Benchmarking buffered writing 456
Finding unreachable Go code 461
Cross-compilation 462
Creating example functions 464
Generating documentation 466
Additional resources 472
Exercises 473
Summary 474
Chapter 12: The Foundations of Network Programming in Go 475
About net/http, net, and http.RoundTripper 476
The http.Response type 476
The http.Request type 477
The http.Transport type 478
About TCP/IP 479
About IPv4 and IPv6 480
The nc(1) command-line utility 480
Reading the configuration of network interfaces 481
Performing DNS lookups 486
Getting the NS records of a domain 488
Getting the MX records of a domain 490
Creating a web server in Go 492
Profiling an HTTP server 495
Creating a website in Go 500
HTTP tracing 510
Testing HTTP handlers 513
Creating a web client in Go 516
Making your Go web client more advanced 518
Timing out HTTP connections 522
More information about SetDeadline 524
Setting the timeout period on the server side 525
Yet another way to time out! 527
Wireshark and tshark tools 529
Additional resources 529
Exercises 530
Summary 531
Chapter 13: Network Programming – Building Servers and Clients 532
The net standard Go package 533
A TCP client 533
A slightly different version of the TCP client 535
A TCP server 537
A slightly different version of the TCP server 539
A UDP client 542
Developing a UDP server 544
A concurrent TCP server 546
A handy concurrent TCP server 551
Remote Procedure Call (RPC) 557
The RPC client 558
The RPC server 559
Doing low-level network programming 561
Grabbing raw ICMP network data 564
Where to go next? 569
Additional resources 569
Exercises 570
Summary 571
Other Books You May Enjoy 572
Index 576
Who this book is for
This book is for amateur and intermediate Go programmers who want to take their Go
knowledge to the next level as well as for experienced developers in other programming
languages who want to learn Go without learning again how a for loop works.
Some of the information found in this book can be also found in my other book, Go Systems
Programming by Packt Publishing. The main difference between these two books is that Go
Systems Programming is about developing system tools using the capabilities of Go, whereas
Mastering Go is about explaining the capabilities and the internals of Go in order to become
a better Go developer. Both books can be used as a reference after reading them for the first
or the second time.