0%

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

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Get in touch
  2. A Quick Introduction to Go
    1. Introducing Go
    2. The history of Go
    3. Why UNIX and not Windows?
    4. The advantages of Go
    5. The go doc and godoc utilities
    6. Hello World!
    7. Introducing functions
    8. Introducing packages
    9. Running Go code
    10. Compiling Go code
    11. Using Go like a scripting language
    12. Important formatting and coding rules
    13. Important characteristics of Go
    14. Defining and using variables
    15. Printing variables
    16. Controlling program flow
    17. Iterating with for loops and range
    18. Getting user input
    19. Reading from standard input
    20. Working with command-line arguments
    21. Using error variables to differentiate between input types
    22. Understanding the Go concurrency model
    23. Developing the which(1) utility in Go
    24. Logging information
    25. log.Fatal() and log.Panic()
    26. Writing to a custom log file
    27. Printing line numbers in log entries
    28. Overview of Go generics
    29. Developing a basic phone book application
    30. Exercises
    31. Summary
    32. Additional resources
  3. Basic Go Data Types
    1. The error data type
    2. Numeric data types
    3. Non-numeric data types
    4. Strings, Characters, and Runes
    5. Converting from int to string
    6. The unicode package
    7. The strings package
    8. Times and dates
    9. A utility for parsing dates and times
    10. Working with different time zones
    11. Go constants
    12. The constant generator iota
    13. Grouping similar data
    14. Arrays
    15. Slices
    16. About slice length and capacity
    17. Selecting a part of a slice
    18. Byte slices
    19. Deleting an element from a slice
    20. How slices are connected to arrays
    21. The copy() function
    22. Sorting slices
    23. Pointers
    24. Generating random numbers
    25. Generating random strings
    26. Generating secure random numbers
    27. Updating the phone book application
    28. Exercises
    29. Summary
    30. Additional resources
  4. Composite Data Types
    1. Maps
    2. Storing to a nil map
    3. Iterating over maps
    4. Structures
    5. Defining new structures
    6. Using the new keyword
    7. Slices of structures
    8. Regular expressions and pattern matching
    9. About Go regular expressions
    10. Matching names and surnames
    11. Matching integers
    12. Matching the fields of a record
    13. Improving the phone book application
    14. Working with CSV files
    15. Adding an index
    16. The improved version of the phone book application
    17. Exercises
    18. Summary
    19. Additional resources
  5. Reflection and Interfaces
    1. Reflection
    2. Learning the internal structure of a Go structure
    3. Changing structure values using reflection
    4. The three disadvantages of reflection
    5. Type methods
    6. Creating type methods
    7. Using type methods
    8. Interfaces
    9. The sort.Interface interface
    10. The empty interface
    11. Type assertions and type switches
    12. The map[string]interface{} map
    13. The error data type
    14. Writing your own interfaces
    15. Using a Go interface
    16. Implementing sort.Interface for 3D shapes
    17. Working with two different CSV file formats
    18. Object-oriented programming in Go
    19. Updating the phone book application
    20. Setting up the value of the CSV file
    21. Using the sort package
    22. Exercises
    23. Summary
    24. Additional resources
  6. Go Packages and Functions
    1. Go packages
    2. Downloading Go packages
    3. Functions
    4. Anonymous functions
    5. Functions that return multiple values
    6. The return values of a function can be named
    7. Functions that accept other functions as parameters
    8. Functions can return other functions
    9. Variadic functions
    10. The defer keyword
    11. Developing your own packages
    12. The init() function
    13. Order of execution
    14. Using GitHub to store Go packages
    15. A package for working with a database
    16. Getting to know your database
    17. Storing the Go package
    18. The design of the Go package
    19. The implementation of the Go package
    20. Testing the Go package
    21. Modules
    22. Creating better packages
    23. Generating documentation
    24. GitLab Runners and Go
    25. The initial version of the configuration file
    26. The final version of the configuration file
    27. GitHub Actions and Go
    28. Storing secrets in GitHub
    29. The final version of the configuration file
    30. Versioning utilities
    31. Exercises
    32. Summary
    33. Additional resources
  7. Telling a UNIX System What to Do
    1. stdin, stdout, and stderr
    2. UNIX processes
    3. Handling UNIX signals
    4. Handling two signals
    5. File I/O
    6. The io.Reader and io.Writer interfaces
    7. Using and misusing io.Reader and io.Writer
    8. Buffered and unbuffered file I/O
    9. Reading text files
    10. Reading a text file line by line
    11. Reading a text file word by word
    12. Reading a text file character by character
    13. Reading from /dev/random
    14. Reading a specific amount of data from a file
    15. Writing to a file
    16. Working with JSON
    17. Using Marshal() and Unmarshal()
    18. Structures and JSON
    19. Reading and writing JSON data as streams
    20. Pretty printing JSON records
    21. Working with XML
    22. Converting JSON to XML and vice versa
    23. Working with YAML
    24. The viper package
    25. Using command-line flags
    26. Reading JSON configuration files
    27. The cobra package
    28. A utility with three commands
    29. Adding command-line flags
    30. Creating command aliases
    31. Creating subcommands
    32. Finding cycles in a UNIX file system
    33. New to Go 1.16
    34. Embedding files
    35. ReadDir and DirEntry
    36. The io/fs package
    37. Updating the phone book application
    38. Using cobra
    39. Storing and loading JSON data
    40. Implementing the delete command
    41. Implementing the insert command
    42. Implementing the list command
    43. Implementing the search command
    44. Exercises
    45. Summary
    46. Additional resources
  8. Go Concurrency
    1. Processes, threads, and goroutines
    2. The Go scheduler
    3. The GOMAXPROCS environment variable
    4. Concurrency and parallelism
    5. Goroutines
    6. Creating a goroutine
    7. Creating multiple goroutines
    8. Waiting for your goroutines to finish
    9. What if the number of Add() and Done() calls differ?
    10. Creating multiple files with goroutines
    11. Channels
    12. Writing to and reading from a channel
    13. Receiving from a closed channel
    14. Channels as function parameters
    15. Race conditions
    16. The Go race detector
    17. The select keyword
    18. Timing out a goroutine
    19. Timing out a goroutine – inside main()
    20. Timing out a goroutine – outside main()
    21. Go channels revisited
    22. Buffered channels
    23. nil channels
    24. Worker pools
    25. Signal channels
    26. Specifying the order of execution for your goroutines
    27. Shared memory and shared variables
    28. The sync.Mutex type
    29. What happens if you forget to unlock a mutex?
    30. The sync.RWMutex type
    31. The atomic package
    32. Sharing memory using goroutines
    33. Closured variables and the go statement
    34. The context package
    35. Using context as a key/value store
    36. The semaphore package
    37. Exercises
    38. Summary
    39. Additional resources
  9. Building Web Services
    1. The net/http package
    2. The http.Response type
    3. The http.Request type
    4. The http.Transport type
    5. Creating a web server
    6. Updating the phone book application
    7. Defining the API
    8. Implementing the handlers
    9. Exposing metrics to Prometheus
    10. The runtime/metrics package
    11. Exposing metrics
    12. Creating a Docker image for a Go server
    13. Exposing the desired metrics
    14. Reading metrics
    15. Putting the metrics in Prometheus
    16. Visualizing Prometheus metrics in Grafana
    17. Developing web clients
    18. Using http.NewRequest() to improve the client
    19. Creating a client for the phone book service
    20. Creating file servers
    21. Downloading the contents of the phone book application
    22. Timing out HTTP connections
    23. Using SetDeadline()
    24. Setting the timeout period on the client side
    25. Setting the timeout period on the server side
    26. Exercises
    27. Summary
    28. Additional resources
  10. Working with TCP/IP and WebSocket
    1. TCP/IP
    2. The nc(1) command-line utility
    3. The net package
    4. Developing a TCP client
    5. Developing a TCP client with net.Dial()
    6. Developing a TCP client that uses net.DialTCP()
    7. Developing a TCP server
    8. Developing a TCP server with net.Listen()
    9. Developing a TCP server that uses net.ListenTCP()
    10. Developing a UDP client
    11. Developing a UDP server
    12. Developing concurrent TCP servers
    13. Working with UNIX domain sockets
    14. A UNIX domain socket server
    15. A UNIX domain socket client
    16. Creating a WebSocket server
    17. The implementation of the server
    18. Using websocat
    19. Using JavaScript
    20. Creating a WebSocket client
    21. Exercises
    22. Summary
    23. Additional resources
  11. Working with REST APIs
    1. An introduction to REST
    2. Developing RESTful servers and clients
    3. A RESTful server
    4. A RESTful client
    5. Creating a functional RESTful server
    6. The REST API
    7. Using gorilla/mux
    8. The use of subrouters
    9. Working with the database
    10. Testing the restdb package
    11. Implementing the RESTful server
    12. Testing the RESTful server
    13. Testing GET handlers
    14. Testing POST handlers
    15. Testing the PUT handler
    16. Testing the DELETE handler
    17. Creating a RESTful client
    18. Creating the structure of the command-line client
    19. Implementing the RESTful client commands
    20. Using the RESTful client
    21. Working with multiple REST API versions
    22. Uploading and downloading binary files
    23. Using Swagger for REST API documentation
    24. Documenting the REST API
    25. Generating the documentation file
    26. Serving the documentation file
    27. Exercises
    28. Summary
    29. Additional resources
  12. Code Testing and Profiling
    1. Optimizing code
    2. Benchmarking code
    3. Rewriting the main() function for better testing
    4. Benchmarking buffered writing and reading
    5. The benchstat utility
    6. Wrongly defined benchmark functions
    7. Profiling code
    8. Profiling a command-line application
    9. Profiling an HTTP server
    10. The web interface of the Go profiler
    11. The go tool trace utility
    12. Tracing a web server from a client
    13. Visiting all routes of a web server
    14. Testing Go code
    15. Writing tests for ./ch03/intRE.go
    16. The TempDir function
    17. The Cleanup() function
    18. The testing/quick package
    19. Timing out tests
    20. Testing code coverage
    21. Finding unreachable Go code
    22. Testing an HTTP server with a database backend
    23. Fuzzing
    24. Cross-compilation
    25. Using go:generate
    26. Creating example functions
    27. Exercises
    28. Summary
    29. Additional resources
  13. Working with gRPC
    1. Introduction to gRPC
    2. Protocol buffers
    3. Defining an interface definition language file
    4. Developing a gRPC server
    5. Developing a gRPC client
    6. Testing the gRPC server with the client
    7. Exercises
    8. Summary
    9. Additional resources
  14. Go Generics
    1. Introducing generics
    2. Constraints
    3. Creating constraints
    4. Defining new data types with generics
    5. Using generics in Go structures
    6. Interfaces versus generics
    7. Reflection versus generics
    8. Exercises
    9. Summary
    10. Additional resources
  15. Appendix A – Go Garbage Collector
    1. Heap and stack
    2. Garbage collection
    3. The tricolor algorithm
    4. More about the operation of the Go garbage collector
    5. Maps, slices, and the Go garbage collector
    6. Using a slice
    7. Using a map with pointers
    8. Using a map without pointers
    9. Splitting the map
    10. Comparing the performance of the presented techniques
    11. Additional resources
  16. Other Books You May Enjoy
  17. Index
34.239.170.244