Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Go Programming Blueprints - Second Edition
Go Programming Blueprints - Second Edition
Go Programming Blueprints - Second Edition
Ebook756 pages5 hours

Go Programming Blueprints - Second Edition

Rating: 4.5 out of 5 stars

4.5/5

()

Read preview

About this ebook

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.
LanguageEnglish
Release dateOct 27, 2016
ISBN9781786462473
Go Programming Blueprints - Second Edition

Related to Go Programming Blueprints - Second Edition

Related ebooks

Internet & Web For You

View More

Related articles

Reviews for Go Programming Blueprints - Second Edition

Rating: 4.5 out of 5 stars
4.5/5

2 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Go Programming Blueprints - Second Edition - Mat Ryer

    Table of Contents

    Go Programming Blueprints Second Edition

    Credits

    About the Author

    Acknowledgments

    About the Reviewer

    www.PacktPub.com

    Why subscribe?

    Preface

    What this book covers

    What you need for this book

    Who this book is for

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Errata

    Piracy

    Questions

    1. Chat Application with Web Sockets

    A simple web server

    Separating views from logic using templates

    Doing things once

    Using your own handlers

    Properly building and executing Go programs

    Modeling a chat room and clients on the server

    Modeling the client

    Modeling a room

    Concurrency programming using idiomatic Go

    Turning a room into an HTTP handler

    Using helper functions to remove complexity

    Creating and using rooms

    Building an HTML and JavaScript chat client

    Getting more out of templates

    Tracing code to get a look under the hood

    Writing a package using TDD

    Interfaces

    Unit tests

    Red-green testing

    Implementing the interface

    Unexported types being returned to users

    Using our new trace package

    Making tracing optional

    Clean package APIs

    Summary

    2. Adding User Accounts

    Handlers all the way down

    Making a pretty social sign-in page

    Endpoints with dynamic paths

    Getting started with OAuth2

    Open source OAuth2 packages

    Tell the authorization providers about your app

    Implementing external logging in

    Logging in

    Handling the response from the provider

    Presenting the user data

    Augmenting messages with additional data

    Summary

    3. Three Ways to Implement Profile Pictures

    Avatars from the OAuth2 server

    Getting the avatar URL

    Transmitting the avatar URL

    Adding the avatar to the user interface

    Logging out

    Making things prettier

    Implementing Gravatar

    Abstracting the avatar URL process

    The auth service and the avatar's implementation

    Using an implementation

    The Gravatar implementation

    Uploading an avatar picture

    User identification

    An upload form

    Handling the upload

    Serving the images

    The Avatar implementation for local files

    Supporting different file types

    Refactoring and optimizing our code

    Replacing concrete types with interfaces

    Changing interfaces in a test-driven way

    Fixing the existing implementations

    Global variables versus fields

    Implementing our new design

    Tidying up and testing

    Combining all three implementations

    Summary

    4. Command-Line Tools to Find Domain Names

    Pipe design for command-line tools

    Five simple programs

    Sprinkle

    Domainify

    Coolify

    Synonyms

    Using environment variables for configuration

    Consuming a web API

    Getting domain suggestions

    Available

    Composing all five programs

    One program to rule them all

    Summary

    5. Building Distributed Systems and Working with Flexible Data

    The system design

    The database design

    Installing the environment

    Introducing NSQ

    NSQ driver for Go

    Introducing MongoDB

    MongoDB driver for Go

    Starting the environment

    Reading votes from Twitter

    Authorization with Twitter

    Extracting the connection

    Reading environment variables

    Reading from MongoDB

    Reading from Twitter

    Signal channels

    Publishing to NSQ

    Gracefully starting and stopping programs

    Testing

    Counting votes

    Connecting to the database

    Consuming messages in NSQ

    Keeping the database updated

    Responding to Ctrl + C

    Running our solution

    Summary

    6. Exposing Data and Functionality through a RESTful Data Web Service API

    RESTful API design

    Sharing data between handlers

    Context keys

    Wrapping handler functions

    API keys

    Cross-origin resource sharing

    Injecting dependencies

    Responding

    Understanding the request

    Serving our API with one function

    Using handler function wrappers

    Handling endpoints

    Using tags to add metadata to structs

    Many operations with a single handler

    Reading polls

    Creating a poll

    Deleting a poll

    CORS support

    Testing our API using curl

    A web client that consumes the API

    Index page showing a list of polls

    Creating a new poll

    Showing the details of a poll

    Running the solution

    Summary

    7. Random Recommendations Web Service

    The project overview

    Project design specifics

    Representing data in code

    Public views of Go structs

    Generating random recommendations

    The Google Places API key

    Enumerators in Go

    Test-driven enumerator

    Querying the Google Places API

    Building recommendations

    Handlers that use query parameters

    CORS

    Testing our API

    Web application

    Summary

    8. Filesystem Backup

    Solution design

    The project structure

    The backup package

    Considering obvious interfaces first

    Testing interfaces by implementing them

    Has the filesystem changed?

    Checking for changes and initiating a backup

    Hardcoding is OK for a short while

    The user command-line tool

    Persisting small data

    Parsing arguments

    Listing the paths

    String representations for your own types

    Adding paths

    Removing paths

    Using our new tool

    The daemon backup tool

    Duplicated structures

    Caching data

    Infinite loops

    Updating filedb records

    Testing our solution

    Summary

    9. Building a Q&A Application for Google App Engine

    The Google App Engine SDK for Go

    Creating your application

    App Engine applications are Go packages

    The app.yaml file

    Running simple applications locally

    Deploying simple applications to Google App Engine

    Modules in Google App Engine

    Specifying modules

    Routing to modules with dispatch.yaml

    Google Cloud Datastore

    Denormalizing data

    Entities and data access

    Keys in Google Cloud Datastore

    Putting data into Google Cloud Datastore

    Reading data from Google Cloud Datastore

    Google App Engine users

    Embedding denormalized data

    Transactions in Google Cloud Datastore

    Using transactions to maintain counters

    Avoiding early abstraction

    Querying in Google Cloud Datastore

    Votes

    Indexing

    Embedding a different view of entities

    Casting a vote

    Accessing parents via datastore.Key

    Line of sight in code

    Exposing data operations over HTTP

    Optional features with type assertions

    Response helpers

    Parsing path parameters

    Exposing functionality via an HTTP API

    HTTP routing in Go

    Context in Google App Engine

    Decoding key strings

    Using query parameters

    Anonymous structs for request data

    Writing self-similar code

    Validation methods that return an error

    Mapping the router handlers

    Running apps with multiple modules

    Testing locally

    Using the admin console

    Automatically generated indexes

    Deploying apps with multiple modules

    Summary

    10. Micro-services in Go with the Go kit Framework

    Introducing gRPC

    Protocol buffers

    Installing protocol buffers

    Protocol buffers language

    Generating Go code

    Building the service

    Starting with tests

    Constructors in Go

    Hashing and validating passwords with bcrypt

    Modeling method calls with requests and responses

    Endpoints in Go kit

    Making endpoints for service methods

    Different levels of error

    Wrapping endpoints into a Service implementation

    An HTTP server in Go kit

    A gRPC server in Go kit

    Translating from protocol buffer types to our types

    Creating a server command

    Using Go kit endpoints

    Running the HTTP server

    Running the gRPC server

    Preventing a main function from terminating immediately

    Consuming the service over HTTP

    Building a gRPC client

    A command-line tool to consume the service

    Parsing arguments in CLIs

    Maintaining good line of sight by extracting case bodies

    Installing tools from the Go source code

    Rate limiting with service middleware

    Middleware in Go kit

    Manually testing the rate limiter

    Graceful rate limiting

    Summary

    11. Deploying Go Applications Using Docker

    Using Docker locally

    Installing Docker tools

    Dockerfile

    Building Go binaries for different architectures

    Building a Docker image

    Running a Docker image locally

    Inspecting Docker processes

    Stopping a Docker instance

    Deploying Docker images

    Deploying to Docker Hub

    Deploying to Digital Ocean

    Creating a droplet

    Accessing the droplet's console

    Pulling Docker images

    Running Docker images in the cloud

    Accessing Docker images in the cloud

    Summary

    Appendix. Good Practices for a Stable Go Environment

    Installing Go

    Configuring Go

    Getting GOPATH right

    Go tools

    Cleaning up, building, and running tests on save

    Integrated developer environments

    Sublime Text 3

    Visual Studio Code

    Summary

    Go Programming Blueprints Second Edition


    Go Programming Blueprints Second Edition

    Copyright © 2016 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    First published: January 2015

    Second edition: October 2016

    Production reference: 1211016

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham 

    B3 2PB, UK.

    ISBN 978-1-78646-894-9

    www.packtpub.com

    Credits

    About the Author

    Mat Ryer has been programming computers since he was 6 years old; he and his father would build games and programs, first in BASIC on a ZX Spectrum and then in AmigaBASIC and AMOS on Commodore Amiga. Many hours were spent on manually copying the code from Amiga Format magazine and tweaking variables or moving GOTO statements around to see what might happen. The same spirit of exploration and obsession with programming led Mat to start work with a local agency in Mansfield, England, when he was 18, where he started to build websites and services.

    In 2006, Mat left rural Nottinghamshire for London, where he took a job at BT. It was here that he worked with a talented group of developers and managers on honing his agile development skills and developing the light flavor that he still uses today.

    After being contracted around London for a few years, coding everything from C# and Objective-C to Ruby and JavaScript, Mat noticed a new systems language called Go that Google was pioneering. Since it addressed very pertinent and relevant modern technical challenges, Mat started using it to solve problems while the language was still in the beta stage and he has used it ever since.

    In 2012, Mat moved to Boulder, Colorado, where he worked on a variety of projects, from big data web services and highly available systems to small side projects and charitable endeavors. He returned home, to London, in 2015 after the company he was working in was sold. Mat, to this day, continues to use Go to build a variety of products, services, and open-source projects. He writes articles about Go on his blog at matryer.com and tweets about Go with the handle @matryer.

    Mat is a regular speaker at Go conferences around the world and encourages people to come up and introduce themselves if their paths ever cross.

    Acknowledgments

    I wouldn't have been able to write this book, or the second edition, without the help of the wonderful Laurie Edwards, who, while working on her own projects took the time to keep me organized and focused. Without her continuous and undying support, I dare say this book (along with every other project I have embarked on) would never have happened. Development heroes of mine include David Hernández (@dahernan on GitHub), who delights in telling me that my ideas are terrible before later falling in love with them; Ernesto Jiménez, who works extremely hard and extremely effectively on private and public projects alike; Tyler Bunnell (@tylerb on GitHub), who I learned Go with; and Ryan Quinn (@mazondo on GitHub), who seems to build an app a day and is living proof of how building something, however simple, is always better than building nothing. Thanks also goes out to Tim Schreiner for engaging in debates with me over the good and bad bits of Go as well as being my go-to guy on matters close to and beyond the fringes of computer science. Thanks go to the core Go team for building such a fun language and to the entire Go community, who have saved me months of development with their contributions. A special shout out to the Women Who Go and Go Bridge (@golangbridge on Twitter) groups, who are working increasingly hard to help us reach and maintain a rich and diversely populated community. Special thanks also goes to everyone who has supported me in my life and helped me in developing what I love into a career, including, but not limited to, Nick and Maggie Ryer, Chris Ryer, Glenn and Tracey Wilson, Phil Jackson, Aaron Edell, Sek Chai, David Hernández, Ernesto Jiménez, Blaine Garst, Tim and Stacey Stockhaus, Tom Szabo, Steve Davis, Mark Gray, John Motz, Rory Donnelly, Piotr Rojek, Corey Prak, Peter Bourgon, Andrew Gerrand, Dave Cheney, William (Bill) Kennedy, Matt Heath, Carlisia Campos, Tiffany Jernigan, Natalie Pistunovich, Simon Howard, Sean Thompson, Jeff Cavins, Edd Grant, Alan Meade, Steve Cart, Andy Jackson, Aditya Pradana, Andy Joslin, Kal Chottai, Tim Ryer, Emma Payne, Corey and Ashton Ryer, Clair Ryer, Gareth and Dylan Evans, Holly Smitheman, Phil Edwards, Tracey Edwards, Kirsten, Megan and Paul Krawczyk, Alex, Adriénne and Ethan Edwards, Chantelle and Greg Rosson, and all my other great friends and family. In the loving memory of Maggie Ryer, 1961 - 2015.

    About the Reviewer

    Michael Hamrah is a software engineer from Brooklyn, New York, specializing in scalable and distributed systems with more than a decade of development experience. He is currently working as a Senior Software Engineer at Uber focusing on metrics and monitoring systems, which handles billions of low-latency events per day across multiple data centers. He works primarily with Go and has an extensive experience with all levels of the software stack . He can be reached on LinkedIn at https://www.linkedin.com/in/hamrah.

    David Hernandez is an independent software engineer from London. He helps companies improve their software. He has worked in different countries, such as Spain, UK, and Australia. He has participated in projects such as the BBC London Olympics 2012. Additionally, he has also helped to achieve Continuous Delivery at Atlassian, and he has delivered services to citizens at UK Government Digital Services.

    You can find David speaking and collaborating at the Go London User Group, as Go is his favorite programming language.

    www.PacktPub.com

    For support files and downloads related to your book, please visit www.PacktPub.com. For support files and downloads related to your book, please visit www.PacktPub.com.

    Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.

    At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

    https://www.packtpub.com/mapt

    Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

    Why subscribe?

    Fully searchable across every book published by Packt

    Copy and paste, print, and bookmark content

    On demand and accessible via a web browser

    Preface

    I have been blown away by the response Go Programming Blueprints has received, both from newcomers to Go, as well as well-respected titans of the community. The positive feedback has inspired me to do this second edition, where the code has been updated to the latest thinking and three new chapters have been added. Thanks to the contributions and questions from readers on the GitHub repository (https://github.com/matryer/goblueprints), I have been able to address some errors, fix some bugs, and clear some things up. See the README file on GitHub for a complete list of their names.

    I decided to write Go Programming Blueprints because I wanted to expel the myth that Go, being a relatively young language and community, is a bad choice to write and iterate on software quickly. I have a friend who knocks out complete Ruby on Rails apps in a weekend by mashing up pre-existing gems and libraries; Rails as a platform has become known for enabling rapid development. As I do the same with Go and the ever-growing buffet of open source packages, I wanted to share some real-world examples of how we can quickly build and release software that performs well from day one and is ready to scale when our projects take off in a way that Rails cannot compete with. Of course, most scalability happens outside the language, but features such as Go's built-in concurrency mean you can get some very impressive results from even the most basic hardware, giving you a head start when things start to get real.

    This book explores some very different projects, any of which can form the basis of a genuine startup. Whether it's a low-latency chat application, a domain name suggestion tool, a social polling and election service built on Twitter, or a random night out generator powered by Google Places, each chapter touches upon a variety of problems that most products or services written in Go will need to address. The solutions I present in this book are just one of many ways to tackle each project, and I will encourage you to make up your own mind about how I approached them. The concepts are more important than the code itself, but you'll hopefully pick up a few tips and tricks here and there that can go into your Go toolbelt.

    New to this second edition, we will explore some practical modern architectural thinking, such as how to build for Google App Engine, what a microservice looks like, and how to package up our code with Docker and deploy to anywhere.

    The process by which I wrote this book may be interesting because it represents something about the philosophies adopted by many agile developers. I started by giving myself the challenge of building a real deployable product (albeit a simple one; a minimum viable product, if you will) before getting stuck into it and writing a version 1. Once I got it working, I would rewrite it from scratch. It is said many times by novelists and journalists that the art of writing is rewriting; I have found this to be true for software as well. The first time we write a piece of code, all we are really doing is learning about the problem and how it might be tackled, as well as getting some of our thinking out of our heads and onto paper, or into a text editor. The second time we write it, we are applying our new knowledge to actually solve the problem. If you've never tried this, give it a shot—you might find that the quality of your code shoots up quite dramatically as I did. It doesn't mean the second time will be the last time—software evolves and we should try to keep it as cheap and disposable as possible so we don't mind throwing pieces away if they go stale or start to get in the way.

    I write all of my code following Test-driven development (TDD) practices, some of which we will do together throughout the book and some you'll just see the result of in the final code. All of the test code can be found in the GitHub repositories for this book, even if it's not included in print.

    Once I had my test-driven second versions completed, I started writing the chapter describing how and why I did what I did. In most cases, the iterative approach I took is left out of the book because it would just add pages of tweaks and edits, which would probably just become frustrating for the reader. However, on a couple of occasions, we will iterate together to get a feel of how a process of gradual improvements and small iterations (starting and keeping it simple and introducing complexity only when absolutely necessary) can be applied when writing Go packages and programs.

    I moved to the United States from England in 2012, but that is not why the chapters are authored in American English; it was a requirement from the publisher. I suppose this book is aimed at an American audience, or perhaps it's because American English is the standard language of computing (in British code, properties that deal with color are spelled without the U). Either way, I apologize in advance for any trans-Atlantic slips; I know how pedantic programmers can be.

    Any questions, improvements, suggestions, or debates (I love how opinionated the Go community—as well as the core team and the language itself—is) are more than welcome. These should probably take place in the GitHub issues for the book setup, specifically at https://github.com/matryer/goblueprints, so that everybody can take part.

    Finally, I would be thrilled if somebody forms a start-up based on any of these projects, or makes use of them in other places. I would love to hear about it; you can tweet me at @matryer.

    What this book covers

    Chapter 1, Chat Application with Web Sockets, shows how to build a complete web application that allows multiple people to have a real-time conversation right in their web browser. We will see how the NET/HTTP package let us serve HTML pages as well as connect to the client's browser with web sockets.

    Chapter 2, Adding User Accounts, shows how to add OAuth to our chat application so that we can keep track of who is saying what, but let them log in using Google, Facebook, or GitHub.

    Chapter 3, Three Ways to Implement Profile Pictures, explains how to add profile pictures to the chat application taken from either the authentication service, the Gravitar.com web service, or by allowing users to upload their own picture from their hard drive.

    Chapter 4, Command-Line Tools to Find Domain Names, explores how easy building command-line tools is in Go and puts those skills to use to tackle the problem of finding the perfect domain name for our chat application. It also explores how easy Go makes it to utilize the standard-in and standard-out pipes to produce some pretty powerful composable tools.

    Chapter 5, Building Distributed Systems and Working with Flexible Data, explains how to prepare for the future of democracy by building a highly-scalable Twitter polling and vote counting engine powered by NSQ and MongoDB.

    Chapter 6, Exposing Data and Functionality through a RESTful Data Web Service API, looks at how to expose the capabilities we built in Chapter 5, Building Distributed Systems and Working with Flexible Data, through a JSON web service, specifically how the wrapping http.HandlerFunc functions give us a powerful pipeline pattern.

    Chapter 7, Random Recommendations Web Service, shows how to consume the Google Places API to generate a location-based random recommendations API that represents a fun way to explore any area. It also explores why it's important to keep internal data structures private, controlling the public view into the same data, as well as how to implement enumerators in Go.

    Chapter 8, Filesystem Backup, helps to build a simple but powerful filesystem backup tool for our code projects and explore interacting with the filesystem using the OS  package from the Go standard library. It also looks at how Go's interfaces allow simple abstractions to yield powerful results.

    Chapter 9, Building a Q&A Application for Google App Engine, shows how to build applications that can be deployed to Google's infrastructure and run at high scale with little to no operational duties for us. The project we build utilizes some of the cloud services available on Google App Engine, including the Google Cloud Datastore—a highly available and extremely fast schema-less data storage option.

    Chapter 10, Micro-services in Go with the Go Kit Framework, explores a modern software architecture paradigm whereby large monolithic applications are broken down into discrete services with a singular focus. The services run independently of each other, allowing them to be individually scaled to meet demand. Go Kit is a software framework that addresses some of the challenges of a microservice architecture while remaining agnostic to the implementation details.

    Chapter 11, Deploying Go Applications Using Docker, looks at how simple it is to build Docker images to package and deploy the application we built in Chapter 9, Building a Q&A Application for Google App Engine. We will write a Dockerfile that describes the image, and use the Docker tools to build the image, which we will then deploy to the Digital Ocean cloud.

    Appendix, Good Practices for a Stable Go Environment, shows how to install Go from scratch on a new machine and discusses some of the environmental options we have and the impact they might have in the future. We will look at a few code editor (or IDE—Integrated Developer Environment) options and also consider how collaboration might influence some of our decisions as well as the impact open sourcing our packages might have.

    What you need for this book

    To compile and run the code from this book, you will need a computer capable of running an operating system that supports the Go toolset, a list of which can be found at https://golang.org/doc/install#requirements.

    Appendix, Good Practices for a Stable Go Environment, has some useful tips to install Go and set up your development environment, including how to work with the GOPATH environment variable.

    Who this book is for

    This book is for all Go programmers, ranging from beginners looking to explore the language by building real projects to expert gophers with an interest in how the language can be applied in interesting ways.

    Conventions

    In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: You will notice that the deployable artifact for your application is the webApp.war file.

    A block of code is set as follows:

    package meander

    type Cost int8

    const (

      _ Cost = iota

      Cost1

      Cost2

      Cost3

      Cost4

      Cost5

    )

    Any command-line input or output is written as follows:

    go build -o project && ./project

    New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: Once you install Xcode, you open Preferences and navigate to the Downloads section

    Note

    Warnings or important notes appear in a box like this.

    Tip

    Tips and tricks appear like this.

    Reader feedback

    Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

    To send us general feedback, simply e-mail feedback@packtpub.com, and mention the book's title in the subject of your message.

    If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

    Customer support

    Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

    Downloading the example code

    You can download the example code files for this book 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.

    You can download the code files by following these steps:

    Log in or register to our website using your e-mail address and password.

    Hover the mouse pointer on the SUPPORT tab at the top.

    Click on Code Downloads & Errata.

    Enter the name of the book in the Search box.

    Select the book for which you're looking to download the code files.

    Choose from the drop-down menu where you purchased this book from.

    Click on Code Download.

    You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

    Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

    WinRAR / 7-Zip for Windows

    Zipeg / iZip / UnRarX for Mac

    7-Zip / PeaZip for Linux

    The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Go-Programming-Blueprints. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

    Errata

    Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

    To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

    Piracy

    Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

    Please contact us at copyright@packtpub.com with a link to the suspected pirated material.

    We appreciate your help in protecting our authors and our ability to bring you valuable content.

    Questions

    If you have a problem with any aspect of this book, you can contact us at questions@packtpub.com, and we will do our best to address the problem.

    Chapter 1.  Chat Application with Web Sockets

    Go is great for writing high-performance, concurrent server applications and tools, and the Web is the perfect medium over which to deliver them. It would be difficult these days to find a gadget that is not web-enabled and this allows us to build a single application that targets almost all platforms and devices.

    Our first project will be a web-based chat application that allows multiple users to have a real-time conversation right in their web browser. Idiomatic Go applications are often composed of many packages, which are organized by having code in different folders, and this is also true of the Go standard library. We will start by building a simple web server using the net/http package, which

    Enjoying the preview?
    Page 1 of 1