Mastering Go: Harness the power of Go to build professional utilities and concurrent servers and services, 3rd Edition
- Length: 682 pages
- Edition: 3
- Language: English
- Publisher: Packt Publishing
- Publication Date: 2021-08-31
- ISBN-10: 1801079315
- ISBN-13: 9781801079310
- Sales Rank: #624791 (See Top 100 Books)
Master key features of Go, including advanced concepts like concurrency and working with JSON, to create and optimize real-world services, network servers, and clients
Key Features
- Third edition of the bestselling guide to advanced Go programming, expanded to cover RESTful servers, the WebSocket protocol, and Go generics
- Use real-world exercises to build high-performance network servers and powerful command line utilities
- Packed with practical examples and utilities to apply to your own development work and administrative tasks
- Clearly explains Go nuances and features to simplify Go development
Book Description
Go is the language of the future for high-performance systems due to its simplicity and clear principles. Mastering Go shows you how to put Go to work on real production systems. This new edition has been updated to include topics like creating RESTful servers and clients, understanding Go generics, and developing gRPC servers and clients.
Mastering Go, Third Edition explores the capabilities of Go in practice. You will become confident with advanced concepts, including concurrency and the operation of the Go Garbage Collector, using Go with Docker, writing powerful command-line utilities, working with JavaScript Object Notation (JSON) data, and interacting with databases. You will also improve your understanding of Go internals to optimize Go code and use data types and data structures in new and unexpected ways.
This Go programming book also covers the nuances and idioms of Go with exercises and resources to fully embed your newly acquired knowledge. Become an expert Go programmer by building Go systems and implementing advanced Go techniques in your projects.
What you will learn
- Use Go in production
- Write reliable, high-performance concurrent code
- Manipulate data structures including slices, arrays, maps, and pointers
- Develop reusable packages with reflection and interfaces
- Become familiar with generics for effective Go programming
- Create concurrent RESTful servers, and build gRPC clients and servers
- Define Go structures for working with JSON data
Who this book is for
This book is for Go programmers with previous coding experience, who are familiar with the basics of the language and want to become expert Go practitioners.
Table of Contents
- A Quick Introduction to Go
- Basic Go Data Types
- Composite Data Types
- Reflection and Interfaces
- Go Packages and Functions
- Telling a UNIX System What to Do
- Go Concurrency
- Building Web Services
- Working with TCP/IP and WebSocket
- Working with REST APIs
- Code Testing and Profiling
- Working with gRPC
- Go Generics
- Appendix
Preface Who this book is for What this book covers To get the most out of this book Get in touch A Quick Introduction to Go Introducing Go The history of Go Why UNIX and not Windows? The advantages of Go The go doc and godoc utilities Hello World! Introducing functions Introducing packages Running Go code Compiling Go code Using Go like a scripting language Important formatting and coding rules Important characteristics of Go Defining and using variables Printing variables Controlling program flow Iterating with for loops and range Getting user input Reading from standard input Working with command-line arguments Using error variables to differentiate between input types Understanding the Go concurrency model Developing the which(1) utility in Go Logging information log.Fatal() and log.Panic() Writing to a custom log file Printing line numbers in log entries Overview of Go generics Developing a basic phone book application Exercises Summary Additional resources Basic Go Data Types The error data type Numeric data types Non-numeric data types Strings, Characters, and Runes Converting from int to string The unicode package The strings package Times and dates A utility for parsing dates and times Working with different time zones Go constants The constant generator iota Grouping similar data Arrays Slices About slice length and capacity Selecting a part of a slice Byte slices Deleting an element from a slice How slices are connected to arrays The copy() function Sorting slices Pointers Generating random numbers Generating random strings Generating secure random numbers Updating the phone book application Exercises Summary Additional resources Composite Data Types Maps Storing to a nil map Iterating over maps Structures Defining new structures Using the new keyword Slices of structures Regular expressions and pattern matching About Go regular expressions Matching names and surnames Matching integers Matching the fields of a record Improving the phone book application Working with CSV files Adding an index The improved version of the phone book application Exercises Summary Additional resources Reflection and Interfaces Reflection Learning the internal structure of a Go structure Changing structure values using reflection The three disadvantages of reflection Type methods Creating type methods Using type methods Interfaces The sort.Interface interface The empty interface Type assertions and type switches The map[string]interface{} map The error data type Writing your own interfaces Using a Go interface Implementing sort.Interface for 3D shapes Working with two different CSV file formats Object-oriented programming in Go Updating the phone book application Setting up the value of the CSV file Using the sort package Exercises Summary Additional resources Go Packages and Functions Go packages Downloading Go packages Functions Anonymous functions Functions that return multiple values The return values of a function can be named Functions that accept other functions as parameters Functions can return other functions Variadic functions The defer keyword Developing your own packages The init() function Order of execution Using GitHub to store Go packages A package for working with a database Getting to know your database Storing the Go package The design of the Go package The implementation of the Go package Testing the Go package Modules Creating better packages Generating documentation GitLab Runners and Go The initial version of the configuration file The final version of the configuration file GitHub Actions and Go Storing secrets in GitHub The final version of the configuration file Versioning utilities Exercises Summary Additional resources Telling a UNIX System What to Do stdin, stdout, and stderr UNIX processes Handling UNIX signals Handling two signals File I/O The io.Reader and io.Writer interfaces Using and misusing io.Reader and io.Writer Buffered and unbuffered file I/O Reading text files Reading a text file line by line Reading a text file word by word Reading a text file character by character Reading from /dev/random Reading a specific amount of data from a file Writing to a file Working with JSON Using Marshal() and Unmarshal() Structures and JSON Reading and writing JSON data as streams Pretty printing JSON records Working with XML Converting JSON to XML and vice versa Working with YAML The viper package Using command-line flags Reading JSON configuration files The cobra package A utility with three commands Adding command-line flags Creating command aliases Creating subcommands Finding cycles in a UNIX file system New to Go 1.16 Embedding files ReadDir and DirEntry The io/fs package Updating the phone book application Using cobra Storing and loading JSON data Implementing the delete command Implementing the insert command Implementing the list command Implementing the search command Exercises Summary Additional resources Go Concurrency Processes, threads, and goroutines The Go scheduler The GOMAXPROCS environment variable Concurrency and parallelism Goroutines Creating a goroutine Creating multiple goroutines Waiting for your goroutines to finish What if the number of Add() and Done() calls differ? Creating multiple files with goroutines Channels Writing to and reading from a channel Receiving from a closed channel Channels as function parameters Race conditions The Go race detector The select keyword Timing out a goroutine Timing out a goroutine – inside main() Timing out a goroutine – outside main() Go channels revisited Buffered channels nil channels Worker pools Signal channels Specifying the order of execution for your goroutines Shared memory and shared variables The sync.Mutex type What happens if you forget to unlock a mutex? The sync.RWMutex type The atomic package Sharing memory using goroutines Closured variables and the go statement The context package Using context as a key/value store The semaphore package Exercises Summary Additional resources Building Web Services The net/http package The http.Response type The http.Request type The http.Transport type Creating a web server Updating the phone book application Defining the API Implementing the handlers Exposing metrics to Prometheus The runtime/metrics package Exposing metrics Creating a Docker image for a Go server Exposing the desired metrics Reading metrics Putting the metrics in Prometheus Visualizing Prometheus metrics in Grafana Developing web clients Using http.NewRequest() to improve the client Creating a client for the phone book service Creating file servers Downloading the contents of the phone book application Timing out HTTP connections Using SetDeadline() Setting the timeout period on the client side Setting the timeout period on the server side Exercises Summary Additional resources Working with TCP/IP and WebSocket TCP/IP The nc(1) command-line utility The net package Developing a TCP client Developing a TCP client with net.Dial() Developing a TCP client that uses net.DialTCP() Developing a TCP server Developing a TCP server with net.Listen() Developing a TCP server that uses net.ListenTCP() Developing a UDP client Developing a UDP server Developing concurrent TCP servers Working with UNIX domain sockets A UNIX domain socket server A UNIX domain socket client Creating a WebSocket server The implementation of the server Using websocat Using JavaScript Creating a WebSocket client Exercises Summary Additional resources Working with REST APIs An introduction to REST Developing RESTful servers and clients A RESTful server A RESTful client Creating a functional RESTful server The REST API Using gorilla/mux The use of subrouters Working with the database Testing the restdb package Implementing the RESTful server Testing the RESTful server Testing GET handlers Testing POST handlers Testing the PUT handler Testing the DELETE handler Creating a RESTful client Creating the structure of the command-line client Implementing the RESTful client commands Using the RESTful client Working with multiple REST API versions Uploading and downloading binary files Using Swagger for REST API documentation Documenting the REST API Generating the documentation file Serving the documentation file Exercises Summary Additional resources Code Testing and Profiling Optimizing code Benchmarking code Rewriting the main() function for better testing Benchmarking buffered writing and reading The benchstat utility Wrongly defined benchmark functions Profiling code Profiling a command-line application Profiling an HTTP server The web interface of the Go profiler The go tool trace utility Tracing a web server from a client Visiting all routes of a web server Testing Go code Writing tests for ./ch03/intRE.go The TempDir function The Cleanup() function The testing/quick package Timing out tests Testing code coverage Finding unreachable Go code Testing an HTTP server with a database backend Fuzzing Cross-compilation Using go:generate Creating example functions Exercises Summary Additional resources Working with gRPC Introduction to gRPC Protocol buffers Defining an interface definition language file Developing a gRPC server Developing a gRPC client Testing the gRPC server with the client Exercises Summary Additional resources Go Generics Introducing generics Constraints Creating constraints Defining new data types with generics Using generics in Go structures Interfaces versus generics Reflection versus generics Exercises Summary Additional resources Appendix A – Go Garbage Collector Heap and stack Garbage collection The tricolor algorithm More about the operation of the Go garbage collector Maps, slices, and the Go garbage collector Using a slice Using a map with pointers Using a map without pointers Splitting the map Comparing the performance of the presented techniques Additional resources Other Books You May Enjoy Index
Donate to keep this site alive
How to download source code?
1. Go to: https://github.com/PacktPublishing
2. In the Find a repository… box, search the book title: Mastering Go: Harness the power of Go to build professional utilities and concurrent servers and services, 3rd Edition
, sometime you may not get the results, please search the main title.
3. Click the book title in the search results.
3. Click Code to download.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.