Go Programming Cookbook - Second Edition

Book description

Tackle the trickiest of problems in Go programming with this practical guide

Key Features

  • Develop applications for different domains using modern programming techniques
  • Tackle common problems when it comes to parallelism, concurrency, and reactive programming in Go
  • Work with ready-to-execute code based on the latest version of Go

Book Description

Go (or Golang) is a statically typed programming language developed at Google. Known for its vast standard library, it also provides features such as garbage collection, type safety, dynamic-typing capabilities, and additional built-in types. This book will serve as a reference while implementing Go features to build your own applications.

This Go cookbook helps you put into practice the advanced concepts and libraries that Golang offers. The recipes in the book follow best practices such as documentation, testing, and vendoring with Go modules, as well as performing clean abstractions using interfaces. You'll learn how code works and the common pitfalls to watch out for. The book covers basic type and error handling, and then moves on to explore applications, such as websites, command-line tools, and filesystems, that interact with users. You'll even get to grips with parallelism, distributed systems, and performance tuning.

By the end of the book, you'll be able to use open source code and concepts in Go programming to build enterprise-class applications without any hassle.

What you will learn

  • Work with third-party Go projects and modify them for your use
  • Write Go code using modern best practices
  • Manage your dependencies with the new Go module system
  • Solve common problems encountered when dealing with backend systems or DevOps
  • Explore the Go standard library and its uses
  • Test, profile, and fine-tune Go applications

Who this book is for

If you're a web developer, programmer, or enterprise developer looking for quick solutions to common and not-so-common problems in Go programming, this book is for you. Basic knowledge of the Go language is assumed.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Go Programming Cookbook Second Edition
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Code in Action
      3. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Get in touch
      1. Reviews
  7. I/O and Filesystems
    1. Technical requirements
    2. Using the common I/O interfaces
      1. How to do it...
      2. How it works...
    3. Using the bytes and strings packages
      1. How to do it...
      2. How it works...
    4. Working with directories and files
      1. How to do it...
      2. How it works...
    5. Working with the CSV format
      1. How to do it...
      2. How it works...
    6. Working with temporary files
      1. How to do it...
      2. How it works...
    7. Working with text/template and html/template
      1. How to do it...
      2. How it works...
  8. Command-Line Tools
    1. Technical requirements
    2. Using command-line flags
      1. How to do it...
      2. How it works...
    3. Using command-line arguments
      1. How to do it...
      2. How it works...
    4. Reading and setting environment variables
      1. How to do it...
      2. How it works...
    5. Configuration using TOML, YAML, and JSON
      1. How to do it...
      2. How it works...
    6. Working with Unix pipes
      1. How to do it...
      2. How it works...
    7. Catching and handling signals
      1. How to do it...
      2. How it works...
    8. An ANSI coloring application
      1. How to do it...
      2. How it works...
  9. Data Conversion and Composition
    1. Technical requirements
    2. Converting data types and interface casting
      1. How to do it...
      2. How it works...
    3. Working with numeric data types using math and math/big
      1. How to do it...
      2. How it works...
    4. Currency conversions and float64 considerations
      1. How to do it...
      2. How it works...
    5. Using pointers and SQL NullTypes for encoding and decoding
      1. How to do it...
      2. How it works...
    6. Encoding and decoding Go data
      1. How to do it...
      2. How it works...
    7. Structure tags and basic reflection in Go
      1. How to do it...
      2. How it works...
    8. Implementing collections via closures
      1. How to do it...
      2. How it works...
  10. Error Handling in Go
    1. Technical requirements
    2. Handling errors and the Error interface
      1. How to do it...
      2. How it works...
    3. Using the pkg/errors package and wrapping errors
      1. How to do it...
      2. How it works...
    4. Using the log package and understanding when to log errors
      1. How to do it...
      2. How it works...
    5. Structured logging with the apex and logrus packages
      1. How to do it...
      2. How it works...
    6. Logging with the context package
      1. How to do it...
      2. How it works...
    7. Using package-level global variables
      1. How to do it...
      2. How it works...
    8. Catching panics for long-running processes
      1. How to do it...
      2. How it works...
  11. Network Programming
    1. Technical requirements
    2. Writing a TCP/IP echo server and client
      1. How to do it...
      2. How it works...
    3. Writing a UDP server and client
      1. How to do it...
      2. How it works...
    4. Working with domain name resolution
      1. How to do it...
      2. How it works...
    5. Working with WebSockets
      1. How to do it...
      2. How it works...
    6. Working with net/rpc for calling remote methods
      1. How to do it...
      2. How it works...
    7. Using net/mail for parsing emails
      1. How to do it...
      2. How it works...
  12. All about Databases and Storage
    1. Using the database/sql package with MySQL
      1. Getting ready
      2. How to do it...
      3. How it works...
    2. Executing a database transaction interface
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Connection pooling, rate limiting, and timeouts for SQL
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Working with Redis
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using NoSQL with MongoDB
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Creating storage interfaces for data portability
      1. Getting ready
      2. How to do it...
      3. How it works...
  13. Web Clients and APIs
    1. Technical requirements
    2. Initializing, storing, and passing http.Client structures
      1. How to do it...
      2. How it works...
    3. Writing a client for a REST API
      1. How to do it...
      2. How it works...
    4. Executing parallel and async client requests
      1. How to do it...
      2. How it works...
    5. Making use of OAuth2 clients
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Implementing an OAuth2 token storage interface
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Wrapping a client in added functionality and function composition
      1. How to do it...
      2. How it works...
    8. Understanding GRPC clients
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Using twitchtv/twirp for RPC
      1. Getting ready
      2. How to do it...
      3. How it works...
  14. Microservices for Applications in Go
    1. Technical requirements
    2. Working with web handlers, requests, and ResponseWriter instances
      1. How to do it...
      2. How it works...
    3. Using structures and closures for stateful handlers
      1. How to do it...
      2. How it works...
    4. Validating input for Go structures and user inputs
      1. How to do it...
      2. How it works...
    5. Rendering and content negotiation
      1. How to do it...
      2. How it works...
    6. Implementing and using middleware
      1. How to do it...
      2. How it works...
    7. Building a reverse proxy application
      1. How to do it...
      2. How it works...
    8. Exporting GRPC as a JSON API
      1. Getting ready
      2. How to do it...
      3. How it works...
  15. Testing Go Code
    1. Technical requirements
    2. Mocking using the standard library
      1. How to do it...
      2. How it works...
    3. Using the Mockgen package to mock interfaces
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using table-driven tests to improve coverage
      1. How to do it...
      2. How it works...
    5. Using third-party testing tools
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Behavior testing using Go
      1. Getting ready
      2. How to do it...
      3. How it works...
  16. Parallelism and Concurrency
    1. Technical requirements
    2. Using channels and the select statement
      1. How to do it...
      2. How it works...
    3. Performing async operations with sync.WaitGroup
      1. How to do it...
      2. How it works...
    4. Using atomic operations and mutex
      1. How to do it...
      2. How it works...
    5. Using the context package
      1. How to do it...
      2. How it works...
    6. Executing state management for channels
      1. How to do it...
      2. How it works...
    7. Using the worker pool design pattern
      1. How to do it...
      2. How it works...
    8. Using workers to create pipelines
      1. How to do it...
      2. How it works...
  17. Distributed Systems
    1. Technical requirements
    2. Using service discovery with Consul
      1. How to do it...
      2. How it works...
    3. Implementing basic consensus using Raft
      1. How to do it...
      2. How it works...
    4. Using containerization with Docker
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Orchestration and deployment strategies
      1. How to do it...
      2. How it works...
    6. Monitoring applications
      1. How to do it...
      2. How it works...
    7. Collecting metrics
      1. Getting ready
      2. How to do it...
      3. How it works...
  18. Reactive Programming and Data Streams
    1. Technical requirements
    2. Using Goflow for data flow programming
      1. How to do it...
      2. How it works...
    3. Using Kafka with Sarama
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using async producers with Kafka
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Connecting Kafka to Goflow
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Writing a GraphQL server in Go
      1. How to do it...
      2. How it works...
  19. Serverless Programming
    1. Go programming on Lambda with Apex
      1. Getting ready
      2. How to do it...
      3. How it works...
    2. Apex serverless logging and metrics
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Google App Engine with Go
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Working with Firebase using firebase.google.com/go
      1. Getting ready
      2. How to do it...
      3. How it works...
  20. Performance Improvements, Tips, and Tricks
    1. Technical requirements
    2. Using the pprof tool
      1. How to do it...
      2. How it works...
    3. Benchmarking and finding bottlenecks
      1. How to do it...
      2. How it works...
    4. Memory allocation and heap management
      1. How to do it...
      2. How it works...
    5. Using fasthttprouter and fasthttp
      1. How to do it...
      2. How it works...
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Go Programming Cookbook - Second Edition
  • Author(s): Aaron Torres
  • Release date: July 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789800982