Go Cookbook

Book description

Bridge the gap between basic understanding of Go and use of its advanced features

About This Book

  • Discover a number of recipes and approaches to develop modern back-end applications
  • Put to use the best practices to combine the recipes for sophisticated parallel tools
  • This book is based on Go 1.8, which is the latest version

Who This Book Is For

This book is for web developers, programmers, and enterprise developers. Basic knowledge of the Go language is assumed. Experience with back-end application development is not necessary, but may help understand the motivation behind some of the recipes.

What You Will Learn

  • Test your application using advanced testing methodologies
  • Develop an awareness of application structures, interface design, and tooling
  • Create strategies for third-party packages, dependencies, and vendoring
  • Get to know tricks on treating data such as collections
  • Handle errors and cleanly pass them along to calling functions
  • Wrap dependencies in interfaces for ease of portability and testing
  • Explore reactive programming design patterns in Go

In Detail

Go (a.k.a. Golang) is a statically-typed programming language first developed at Google. It is derived from C with additional features such as garbage collection, type safety, dynamic-typing capabilities, additional built-in types, and a large standard library.

This book takes off where basic tutorials on the language leave off. You can immediately put into practice some of the more advanced concepts and libraries offered by the language while avoiding some of the common mistakes for new Go developers.

The book covers basic type and error handling. It explores applications that interact with users, such as websites, command-line tools, or via the file system. It demonstrates how to handle advanced topics such as parallelism, distributed systems, and performance tuning. Lastly, it finishes with reactive and serverless programming in Go.

Style and approach

This guide is a handy reference for developers to quickly look up Go development patterns. It is a companion to other resources and a reference that will be useful long after reading it through the first time. Each recipe includes working, simple, and tested code that can be used as a reference or foundation for your own applications.

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Conventions
    6. Reader feedback
    7. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. I/O and File Systems
    1. Introduction
    2. Using the common I/O interfaces
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using the bytes and strings packages
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Working with directories and files
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Working with the CSV format
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Working with temporary files
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Working with text/template and HTML/templates
      1. Getting ready
      2. How to do it...
      3. How it works...
  3. Command-Line Tools
    1. Introduction
    2. Using command-line flags
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using command-line arguments
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Reading and setting environment variables
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Configuration using TOML, YAML, and JSON
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Working with Unix pipes
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Catching and handling signals
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. An ANSI coloring application
      1. Getting ready
      2. How to do it...
      3. How it works...
  4. Data Conversion and Composition
    1. Introduction
    2. Converting data types and interface casting
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Working with numeric data types using math and math/big
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Currency conversions and float64 considerations
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using pointers and SQL NullTypes for encoding and decoding
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Encoding and decoding Go data
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Struct tags and basic reflection in Go
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Implementing collections via closures
      1. Getting ready
      2. How to do it...
      3. How it works...
  5. Error Handling in Go
    1. Introduction
    2. Handling errors and the Error interface
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using the pkg/errors package and wrapping errors
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using the log package and understanding when to log errors
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Structured logging with the apex and logrus packages
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Logging with the context package
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using package-level global variables
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Catching panics for long running processes
      1. Getting ready
      2. How to do it...
      3. How it works...
  6. All about Databases and Storage
    1. Introduction
    2. The database/sql package with MySQL
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Executing a database transaction interface
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Connection pooling, rate limiting, and timeouts for SQL
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Working with Redis
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Using NoSQL with MongoDB and mgo
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Creating storage interfaces for data portability
      1. Getting ready
      2. How to do it...
      3. How it works...
  7. Web Clients and APIs
    1. Introduction
    2. Initializing, storing, and passing http.Client structs
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Writing a client for a REST API
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Executing parallel and async client requests
      1. Getting ready
      2. How to do it...
      3. 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. Getting ready
      2. How to do it...
      3. How it works...
    8. Understanding GRPC clients
      1. Getting ready
      2. How to do it...
      3. How it works...
  8. Microservices for Applications in Go
    1. Introduction
    2. Working with web handlers, requests, and ResponseWriters
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using structs and closures for stateful handlers
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Validating input for Go structs and user inputs
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Rendering and content negotiation
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Implementing and using middleware
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Building a reverse proxy application
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Exporting GRPC as a JSON API
      1. Getting ready
      2. How to do it...
      3. How it works...
  9. Testing
    1. Introduction
    2. Mocking using the standard library
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using the Mockgen package
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using table-driven tests to improve coverage
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using third-party testing tools
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Practical fuzzing
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Behavior testing using Go
      1. Getting ready
      2. How to do it...
      3. How it works...
  10. Parallelism and Concurrency
    1. Introduction
    2. Using channels and the select statement
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Performing async operations with sync.WaitGroup
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using atomic operations and mutex
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using the context package
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Executing state management for channels
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using the worker pool design pattern
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Using workers to create pipelines
      1. Getting ready
      2. How to do it...
      3. How it works...
  11. Distributed Systems
    1. Introduction
    2. Using service discovery with Consul
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Implementing basic consensus using Raft
      1. Getting ready
      2. How to do it...
      3. 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. Getting ready
      2. How to do it...
      3. How it works...
    6. Monitoring applications
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Collecting metrics
      1. Getting ready
      2. How to do it...
      3. How it works...
  12. Reactive Programming and Data Streams
    1. Introduction
    2. Goflow for dataflow programming
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Reactive programming with RxGo
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using Kafka with Sarama
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using async producers with Kafka
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Connecting Kafka to Goflow
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Writing a GraphQL server in Go
      1. Getting ready
      2. How to do it...
      3. How it works...
  13. Serverless Programming
    1. Introduction
    2. Go programming on Lambda with Apex
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Apex serverless logging and metrics
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Google App Engine with Go
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Working with Firebase using zabawaba99/firego
      1. Getting ready
      2. How to do it...
      3. How it works...
  14. Performance Improvements, Tips, and Tricks
    1. Introduction
    2. Speeding up compilation and testing cycles
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Using the pprof tool
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Benchmarking and finding bottlenecks
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Memory allocation and heap management
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Vendoring and project layout
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Using fasthttprouter and fasthttp
      1. Getting ready
      2. How to do it...
      3. How it works...

Product information

  • Title: Go Cookbook
  • Author(s): Aaron Torres
  • Release date: June 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781783286836