O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Go Programming Blueprints - Second Edition

Book Description

Build real-world, production-ready solutions in Go using cutting-edge technology and techniques

About This Book

  • Get up to date with Go and write code capable of delivering massive world-class scale performance and availability
  • Learn to apply the nuances of the Go language, and get to know the open source community that surrounds it to implement a wide range of start-up quality projects
  • Write interesting and clever but simple code, and learn skills and techniques that are directly transferrable to your own projects

Who This Book Is For

If you are familiar with Go and are want to put your knowledge to work, then this is the book for you. Go programming knowledge is a must.

What You Will Learn

  • Build quirky and fun projects from scratch while exploring patterns, practices, and techniques, as well as a range of different technologies
  • Create websites and data services capable of massive scale using Go’s net/http package, exploring RESTful patterns as well as low-latency WebSocket APIs
  • Interact with a variety of remote web services to consume capabilities ranging from authentication and authorization to a fully functioning thesaurus
  • Develop high-quality command-line tools that utilize the powerful shell capabilities and perform well using Go’s in-built concurrency mechanisms
  • Build microservices for larger organizations using the Go Kit library
  • Implement a modern document database as well as high-throughput messaging queue technology to put together an architecture that is truly ready to scale
  • Write concurrent programs and gracefully manage the execution of them and communication by smartly using channels
  • Get a feel for app deployment using Docker and Google App Engine

In Detail

Go is the language of the Internet age, and the latest version of Go comes with major architectural changes. Implementation of the language, runtime, and libraries has changed significantly. The compiler and runtime are now written entirely in Go. The garbage collector is now concurrent and provides dramatically lower pause times by running in parallel with other Go routines when possible.

This book will show you how to leverage all the latest features and much more. This book shows you how to build powerful systems and drops you into real-world situations. You will learn to develop high-quality command-line tools that utilize the powerful shell capabilities and perform well using Go’s in-built concurrency mechanisms. Scale, performance, and high availability lie at the heart of our projects, and the lessons learned throughout this book will arm you with everything you need to build world-class solutions. You will get a feel for app deployment using Docker and Google App Engine. Each project could form the basis of a start-up, which means they are directly applicable to modern software markets.

Style and approach

This book provides fun projects that involve building applications from scratch. These projects will teach you to build chat applications, a distributed system, and a recommendation system.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Go Programming Blueprints Second Edition
    1. Go Programming Blueprints Second Edition
    2. Credits
    3. About the Author
    4. Acknowledgments
    5. About the Reviewer
    6. www.PacktPub.com
      1. Why subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Chat Application with Web Sockets
      1. A simple web server
        1. Separating views from logic using templates
          1. Doing things once
          2. Using your own handlers
        2. Properly building and executing Go programs
      2. Modeling a chat room and clients on the server
        1. Modeling the client
        2. Modeling a room
        3. Concurrency programming using idiomatic Go
        4. Turning a room into an HTTP handler
        5. Using helper functions to remove complexity
        6. Creating and using rooms
      3. Building an HTML and JavaScript chat client
        1. Getting more out of templates
      4. Tracing code to get a look under the hood
        1. Writing a package using TDD
        2. Interfaces
        3. Unit tests
          1. Red-green testing
        4. Implementing the interface
          1. Unexported types being returned to users
        5. Using our new trace package
        6. Making tracing optional
        7. Clean package APIs
      5. Summary
    9. 2. Adding User Accounts
      1. Handlers all the way down
      2. Making a pretty social sign-in page
      3. Endpoints with dynamic paths
      4. Getting started with OAuth2
        1. Open source OAuth2 packages
      5. Tell the authorization providers about your app
      6. Implementing external logging in
        1. Logging in
        2. Handling the response from the provider
        3. Presenting the user data
        4. Augmenting messages with additional data
      7. Summary
    10. 3. Three Ways to Implement Profile Pictures
      1. Avatars from the OAuth2 server
        1. Getting the avatar URL
        2. Transmitting the avatar URL
        3. Adding the avatar to the user interface
        4. Logging out
        5. Making things prettier
      2. Implementing Gravatar
        1. Abstracting the avatar URL process
          1. The auth service and the avatar's implementation
          2. Using an implementation
          3. The Gravatar implementation
      3. Uploading an avatar picture
        1. User identification
        2. An upload form
        3. Handling the upload
        4. Serving the images
        5. The Avatar implementation for local files
          1. Supporting different file types
        6. Refactoring and optimizing our code
          1. Replacing concrete types with interfaces
          2. Changing interfaces in a test-driven way
          3. Fixing the existing implementations
          4. Global variables versus fields
          5. Implementing our new design
          6. Tidying up and testing
      4. Combining all three implementations
      5. Summary
    11. 4. Command-Line Tools to Find Domain Names
      1. Pipe design for command-line tools
      2. Five simple programs
        1. Sprinkle
        2. Domainify
        3. Coolify
        4. Synonyms
          1. Using environment variables for configuration
          2. Consuming a web API
          3. Getting domain suggestions
        5. Available
      3. Composing all five programs
        1. One program to rule them all
      4. Summary
    12. 5. Building Distributed Systems and Working with Flexible Data
      1. The system design
        1. The database design
      2. Installing the environment
        1. Introducing NSQ
          1. NSQ driver for Go
        2. Introducing MongoDB
          1. MongoDB driver for Go
        3. Starting the environment
      3. Reading votes from Twitter
        1. Authorization with Twitter
          1. Extracting the connection
          2. Reading environment variables
        2. Reading from MongoDB
        3. Reading from Twitter
          1. Signal channels
        4. Publishing to NSQ
        5. Gracefully starting and stopping programs
        6. Testing
      4. Counting votes
        1. Connecting to the database
        2. Consuming messages in NSQ
        3. Keeping the database updated
        4. Responding to Ctrl + C
      5. Running our solution
      6. Summary
    13. 6. Exposing Data and Functionality through a RESTful Data Web Service API
      1. RESTful API design
      2. Sharing data between handlers
        1. Context keys
      3. Wrapping handler functions
        1. API keys
        2. Cross-origin resource sharing
      4. Injecting dependencies
      5. Responding
      6. Understanding the request
      7. Serving our API with one function
        1. Using handler function wrappers
      8. Handling endpoints
        1. Using tags to add metadata to structs
        2. Many operations with a single handler
          1. Reading polls
          2. Creating a poll
          3. Deleting a poll
          4. CORS support
        3. Testing our API using curl
      9. A web client that consumes the API
        1. Index page showing a list of polls
        2. Creating a new poll
        3. Showing the details of a poll
      10. Running the solution
      11. Summary
    14. 7. Random Recommendations Web Service
      1. The project overview
        1. Project design specifics
      2. Representing data in code
        1. Public views of Go structs
      3. Generating random recommendations
        1. The Google Places API key
        2. Enumerators in Go
          1. Test-driven enumerator
        3. Querying the Google Places API
        4. Building recommendations
        5. Handlers that use query parameters
        6. CORS
        7. Testing our API
          1. Web application
      4. Summary
    15. 8. Filesystem Backup
      1. Solution design
        1. The project structure
      2. The backup package
        1. Considering obvious interfaces first
        2. Testing interfaces by implementing them
        3. Has the filesystem changed?
        4. Checking for changes and initiating a backup
          1. Hardcoding is OK for a short while
      3. The user command-line tool
        1. Persisting small data
        2. Parsing arguments
          1. Listing the paths
            1. String representations for your own types
          2. Adding paths
          3. Removing paths
        3. Using our new tool
      4. The daemon backup tool
        1. Duplicated structures
        2. Caching data
        3. Infinite loops
        4. Updating filedb records
      5. Testing our solution
      6. Summary
    16. 9. Building a Q&A Application for Google App Engine
      1. The Google App Engine SDK for Go
        1. Creating your application
        2. App Engine applications are Go packages
        3. The app.yaml file
        4. Running simple applications locally
        5. Deploying simple applications to Google App Engine
        6. Modules in Google App Engine
          1. Specifying modules
          2. Routing to modules with dispatch.yaml
      2. Google Cloud Datastore
        1. Denormalizing data
      3. Entities and data access
        1. Keys in Google Cloud Datastore
        2. Putting data into Google Cloud Datastore
        3. Reading data from Google Cloud Datastore
      4. Google App Engine users
        1. Embedding denormalized data
      5. Transactions in Google Cloud Datastore
        1. Using transactions to maintain counters
        2. Avoiding early abstraction
      6. Querying in Google Cloud Datastore
      7. Votes
        1. Indexing
        2. Embedding a different view of entities
      8. Casting a vote
        1. Accessing parents via datastore.Key
        2. Line of sight in code
      9. Exposing data operations over HTTP
        1. Optional features with type assertions
        2. Response helpers
        3. Parsing path parameters
        4. Exposing functionality via an HTTP API
          1. HTTP routing in Go
        5. Context in Google App Engine
        6. Decoding key strings
          1. Using query parameters
          2. Anonymous structs for request data
          3. Writing self-similar code
          4. Validation methods that return an error
        7. Mapping the router handlers
      10. Running apps with multiple modules
        1. Testing locally
          1. Using the admin console
            1. Automatically generated indexes
      11. Deploying apps with multiple modules
      12. Summary
    17. 10. Micro-services in Go with the Go kit Framework
      1. Introducing gRPC
      2. Protocol buffers
        1. Installing protocol buffers
        2. Protocol buffers language
        3. Generating Go code
      3. Building the service
        1. Starting with tests
        2. Constructors in Go
        3. Hashing and validating passwords with bcrypt
      4. Modeling method calls with requests and responses
        1. Endpoints in Go kit
          1. Making endpoints for service methods
          2. Different levels of error
          3. Wrapping endpoints into a Service implementation
      5. An HTTP server in Go kit
      6. A gRPC server in Go kit
        1. Translating from protocol buffer types to our types
      7. Creating a server command
        1. Using Go kit endpoints
        2. Running the HTTP server
        3. Running the gRPC server
        4. Preventing a main function from terminating immediately
        5. Consuming the service over HTTP
      8. Building a gRPC client
        1. A command-line tool to consume the service
        2. Parsing arguments in CLIs
        3. Maintaining good line of sight by extracting case bodies
        4. Installing tools from the Go source code
      9. Rate limiting with service middleware
        1. Middleware in Go kit
        2. Manually testing the rate limiter
        3. Graceful rate limiting
      10. Summary
    18. 11. Deploying Go Applications Using Docker
      1. Using Docker locally
        1. Installing Docker tools
        2. Dockerfile
        3. Building Go binaries for different architectures
        4. Building a Docker image
        5. Running a Docker image locally
        6. Inspecting Docker processes
        7. Stopping a Docker instance
      2. Deploying Docker images
        1. Deploying to Docker Hub
      3. Deploying to Digital Ocean
        1. Creating a droplet
        2. Accessing the droplet's console
        3. Pulling Docker images
        4. Running Docker images in the cloud
        5. Accessing Docker images in the cloud
      4. Summary
    19. Appendix. Good Practices for a Stable Go Environment
      1. Installing Go
      2. Configuring Go
        1. Getting GOPATH right
      3. Go tools
      4. Cleaning up, building, and running tests on save
      5. Integrated developer environments
        1. Sublime Text 3
        2. Visual Studio Code
      6. Summary