You are previewing Go Programming Blueprints.
O'Reilly logo
Go Programming Blueprints

Book Description

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

In Detail

Dive headfirst into solving actual enterprise problems and start cutting code from the word go. You will build complete applications around a variety of subjects using a range of different technologies and techniques, all of which are directly applicable to today's tech start-up world.

Scale, performance, and high availability lie at the heart of the projects, and the lessons learned throughout this book will arm you with everything you need to build world-class solutions. Well-designed applications and sensible architectures can scale horizontally to cope with the demands of up to millions of users, while avoiding the prohibitive up-front investment that can bring a project to its knees.

What You Will Learn

  • Build quirky and fun projects from scratch while exploring widely applicable skills, 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 have great performance using Go's in-built concurrency mechanisms

  • Implement a modern document database as well as high-throughput messaging queue technology to put together an architecture that is truly ready for scale

  • Write concurrent programs and gracefully manage their execution and communication with smart use of channels

  • 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 files e-mailed directly to you.

    Table of Contents

    1. Go Programming Blueprints
      1. Table of Contents
      2. Go Programming Blueprints
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. 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
      9. 1. Chat Application with Web Sockets
        1. A simple web server
          1. 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. Use 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
            1. Interfaces
            2. Unit tests
            3. Red-green testing
            4. Implementing the interface
            5. Unexported types being returned to users
          2. Using our new trace package
          3. Making tracing optional
          4. Clean package APIs
        5. Summary
      10. 2. Adding Authentication
        1. Handlers all the way down
        2. Making a pretty social sign-in page
        3. Endpoints with dynamic paths
        4. OAuth2
          1. Open source OAuth2 packages
        5. Tell the authentication 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
      11. 3. Three Ways to Implement Profile Pictures
        1. Avatars from the authentication 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 authentication service and avatar's implementation
            2. Using an implementation
            3. 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 existing implementations
            4. Global variables versus fields
            5. Implementing our new design
            6. Tidying up and testing
        4. Combining all three implementations
        5. Summary
      12. 4. Command-line Tools to Find Domain Names
        1. Pipe design for command-line tools
        2. Five simple programs
          1. Sprinkle
            1. Exercise – configurable transformations
          2. Domainify
            1. Exercise – making top-level domains configurable
          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
      13. 5. Building Distributed Systems and Working with Flexible Data
        1. System design
          1. Database design
        2. Installing the environment
          1. NSQ
            1. NSQ driver for Go
          2. MongoDB
            1. MongoDB driver for Go
          3. Starting the environment
        3. 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
          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
      14. 6. Exposing Data and Functionality through a RESTful Data Web Service API
        1. RESTful API design
        2. Sharing data between handlers
        3. Wrapping handler functions
          1. API key
          2. Database session
          3. Per request variables
          4. Cross-browser resource sharing
        4. Responding
        5. Understanding the request
        6. A simple main function to serve our API
          1. Using handler function wrappers
        7. 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
        8. A web client that consumes the API
          1. An index page showing a list of polls
          2. A page to create a new poll
          3. A page to show details of the poll
        9. Running the solution
        10. Summary
      15. 7. Random Recommendations Web Service
        1. Project overview
          1. Project design specifics
        2. Representing data in code
          1. Public views of Go structs
        3. Generating random recommendations
          1. 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
      16. 8. Filesystem Backup
        1. Solution design
          1. Project structure
        2. Backup package
          1. Obvious interfaces?
          2. Implementing ZIP
          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
      17. A. Good Practices for a Stable Go Environment
        1. Installing Go
          1. Installing the C tools
          2. Downloading and building Go from the source
        2. Configuring Go
          1. Getting GOPATH right
        3. Go tools
        4. Cleaning up, building, and running tests on save
          1. Sublime Text 3
        5. Summary
      18. Index