Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
.Go Programming Blueprints
.Go Programming Blueprints

.Go Programming Blueprints: Build real-world, production-ready solutions in Go using cutting-edge technology and techniques , Second Edition

eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

.Go Programming Blueprints

Chapter 2.  Adding User Accounts

The chat application we built in the previous chapter focused on high performance transmission of messages from the clients to the server and back again. However, the way things stand, our users have no way of knowing who they would be talking to. One solution to this problem is building some kind of sign-up and login functionality and letting our users create accounts and authenticate themselves before they can open the chat page.

Whenever we are about to build something from scratch, we must ask ourselves how others have solved this problem before (it is extremely rare to encounter genuinely original problems) and whether any open solutions or standards already exist that we can make use of. Authorization and authentication can hardly be considered new problems, especially in the world of the Web, with many different protocols out there to choose from. So how do we decide the best option to pursue? As always, we must look at this question from...

Handlers all the way down

For our chat application, we implemented our own http.Handler type (the room) in order to easily compile, execute, and deliver HTML content to browsers. Since this is a very simple but powerful interface, we are going to continue to use it wherever possible when adding functionality to our HTTP processing.

In order to determine whether a user is allowed to proceed, we will create an authorization wrapper handler that will perform the check and pass the execution on to the inner handler only if the user is authorized.

Our wrapper handler will satisfy the same http.Handler interface as the object inside it, allowing us to wrap any valid handler. In fact, even the authentication handler we are about to write could be later encapsulated inside a similar wrapper if required.

Handlers all the way down

Chaining pattern when applied to HTTP handlers

The preceding diagram shows how this pattern could be applied in a more complicated HTTP handler scenario. Each object implements the http.Handler interface...

Making a pretty social sign-in page

So far, we haven't paid much attention to making our application look nice; after all, this book is about Go and not user interface development. However, there is no excuse for building ugly apps, and so we will build a social sign-in page that is as pretty as it is functional.

Bootstrap is a frontend framework for developing responsive projects on the Web. It provides CSS and JavaScript code that solve many user interface problems in a consistent and good-looking way. While sites built using Bootstrap tend to look the same (although there are a plenty of ways in which the UI can be customized), it is a great choice for early versions of apps or for developers who don't have access to designers.

Tip

If you build your application using the semantic standards set forth by Bootstrap, it will become easy for you to make a Bootstrap theme for your site or application, and you know it will slot right into your code.

We will use the version of Bootstrap...

Endpoints with dynamic paths

Pattern matching for the http package in the Go standard library isn't the most comprehensive and fully featured implementation out there. For example, Ruby on Rails makes it much easier to have dynamic segments inside the path. You could map the route like this:

"auth/:action/:provider_name" 

Rails then provides a data map (or dictionary) containing the values that it automatically extracted from the matched path. So if you visit auth/login/google, then params[:provider_name] would equal google and params[:action] would equal login.

The most the http package lets us specify by default is a path prefix, which we can make use of by leaving a trailing slash at the end of the pattern:

"auth/" 

We would then have to manually parse the remaining segments to extract the appropriate data. This is acceptable for relatively simple cases. This suits our needs for the time being since we only need to handle a few different paths, such as the following...

Getting started with OAuth2

OAuth2 is an open authorization standard designed to allow resource owners to give clients delegated access to private data (such as wall posts or tweets) via an access token exchange handshake. Even if you do not wish to access the private data, OAuth2 is a great option that allows people to sign in using their existing credentials, without exposing those credentials to a third-party site. In this case, we are the third party, and we want to allow our users to sign in using services that support OAuth2.

From a user's point of view, the OAuth2 flow is as follows:

  1. The user selects the provider with whom they wish to sign in to the client app.
  2. The user is redirected to the provider's website (with a URL that includes the client app ID) where they are asked to give permission to the client app.
  3. The user signs in from the OAuth2 service provider and accepts the permissions requested by the third-party application.
  4. The user is redirected to the client app with...

Handlers all the way down


For our chat application, we implemented our own http.Handler type (the room) in order to easily compile, execute, and deliver HTML content to browsers. Since this is a very simple but powerful interface, we are going to continue to use it wherever possible when adding functionality to our HTTP processing.

In order to determine whether a user is allowed to proceed, we will create an authorization wrapper handler that will perform the check and pass the execution on to the inner handler only if the user is authorized.

Our wrapper handler will satisfy the same http.Handler interface as the object inside it, allowing us to wrap any valid handler. In fact, even the authentication handler we are about to write could be later encapsulated inside a similar wrapper if required.

Chaining pattern when applied to HTTP handlers

The preceding diagram shows how this pattern could be applied in a more complicated HTTP handler scenario. Each object implements the http.Handler interface...

Making a pretty social sign-in page


So far, we haven't paid much attention to making our application look nice; after all, this book is about Go and not user interface development. However, there is no excuse for building ugly apps, and so we will build a social sign-in page that is as pretty as it is functional.

Bootstrap is a frontend framework for developing responsive projects on the Web. It provides CSS and JavaScript code that solve many user interface problems in a consistent and good-looking way. While sites built using Bootstrap tend to look the same (although there are a plenty of ways in which the UI can be customized), it is a great choice for early versions of apps or for developers who don't have access to designers.

Tip

If you build your application using the semantic standards set forth by Bootstrap, it will become easy for you to make a Bootstrap theme for your site or application, and you know it will slot right into your code.

We will use the version of Bootstrap hosted on...

Endpoints with dynamic paths


Pattern matching for the http package in the Go standard library isn't the most comprehensive and fully featured implementation out there. For example, Ruby on Rails makes it much easier to have dynamic segments inside the path. You could map the route like this:

"auth/:action/:provider_name" 

Rails then provides a data map (or dictionary) containing the values that it automatically extracted from the matched path. So if you visit auth/login/google, then params[:provider_name] would equal google and params[:action] would equal login.

The most the http package lets us specify by default is a path prefix, which we can make use of by leaving a trailing slash at the end of the pattern:

"auth/" 

We would then have to manually parse the remaining segments to extract the appropriate data. This is acceptable for relatively simple cases. This suits our needs for the time being since we only need to handle a few different paths, such as the following:

  • /auth/login/google

  • ...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

Description

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.

Who is this book 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
  • • Build micro-services 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 27, 2016
Length: 394 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786468949
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Oct 27, 2016
Length: 394 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786468949
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 125.97
Learning Go Programming
€41.99
.Go Programming Blueprints
€41.99
Go Design Patterns
€41.99
Total 125.97 Stars icon

Table of Contents

11 Chapters
1. Chat Application with Web Sockets Chevron down icon Chevron up icon
2. Adding User Accounts Chevron down icon Chevron up icon
3. Three Ways to Implement Profile Pictures Chevron down icon Chevron up icon
4. Command-Line Tools to Find Domain Names Chevron down icon Chevron up icon
5. Building Distributed Systems and Working with Flexible Data Chevron down icon Chevron up icon
6. Exposing Data and Functionality through a RESTful Data Web Service API Chevron down icon Chevron up icon
7. Random Recommendations Web Service Chevron down icon Chevron up icon
8. Filesystem Backup Chevron down icon Chevron up icon
9. Building a Q&A Application for Google App Engine Chevron down icon Chevron up icon
10. Micro-services in Go with the Go kit Framework Chevron down icon Chevron up icon
11. Deploying Go Applications Using Docker Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9
(12 Ratings)
5 star 58.3%
4 star 8.3%
3 star 8.3%
2 star 16.7%
1 star 8.3%
Filter icon Filter
Top Reviews

Filter reviews by




Mr Daniel T. Morgan Mar 21, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book was just the tonic I needed in my Go journey. You've learned the language, you've started to solve problems, but you are not quite sure which way to skin that cat. This book is the logical step in that journey. I'm only 30% through and I already have plenty of ideas how to refactor what I've done and how to be more productive in some projects I'm starting imminently.
Amazon Verified review Amazon
Amazon Customer Dec 14, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am a new comer to the world of Go. When I started learning, there was not so much material that provides you this in-depth knowledge of concepts. I felt this gap, after learning the basic syntax, and having a foundation of how things work in Go.Go Programming Blueprints covered this gap for me, after learning the basics of Go from the different resources, this book helped me take a step further and understand and see things from a different perspective.You can hack your way into Go from different tutorials, but this book gives you a structured approach and makes sure you grasp many of the concepts that you might miss, specially if you are new to programming.This book is a must have, if you want a solid understanding of Go.
Amazon Verified review Amazon
Eddy Hernández Feb 01, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I really love this book, I'm new in Go language but the Author it takes the time to explain each example, also you can get feedback creating a new issue on Github repo and the important think is you will learn the cutting edge technologies that the author offers.Ready to start the third chapter.
Amazon Verified review Amazon
Zakatell Kanda Jun 05, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Awesome Go Book targeted for intermediate developers in another language. I couldn't be more happier when I read this book, exactly what I needed. Plus the accompanying blog post is also a nice touch for updated knowledge.If your new to Golang, I suggest reading the book: The Go Programming Language
Amazon Verified review Amazon
Waggdog Jul 29, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
All of my books on Go are good to great, this one is great, precise concise and clear language denotes, I think, a superior grasp of the topic - which lets face it, is, for most of us, abstract. This book is packed (no pun intended) with insight. Thank you Mat, appreciate the care given.... Seems that Go libraries are progressing rapidly and best practice today is different to what it was a year or two back. This book appears to be a little more up to date than my others. Thanks again.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.