Contents at a Glance
Contents
About the Author
About the Technical Reviewer
Introduction
Chapter 1: Getting Started with Go
Introducing Go
Minimalistic Language with Pragmatic Design
A Static Type Language with High Productivity
Concurrency Is a Built-In Feature at the Language Level
Go Compiles Programs Quickly
Go as a General-Purpose Language
Go Ecosystem
Installing the Go Tools
Checking the Installation
Setting up a Work Environment
Go Workspace
GOPATH Environment Variable
Code Organization Paths
Writing Go Programs
Writing a Hello World Program
Writing a Library
Testing Go Code
Using Go Playground
Using Go Mobile
Go as a Language for Web and Microservices
Summary
Chapter 2: Go Fundamentals
Packages
Package main
Package Alias
Function init
Using a Blank Identifier
Importing Packages
Install Third-Party Packages
Writing Packages
Go Tool
Formatting Go Code
Go Documentation
Working with Collections
Arrays
Slices
Creating a Slice with the make Function
Creating a Slice with Slice Literal
Slice Functions
Length and Capacity
Iterating Over Slices
Maps
Defer, Panic, and Recover
Defer
Panic
Recover
Error Handling
Summary
Chapter 3: User-Defined Types and Concurrency
User-defined Types with Structs
Creating a Struct Type
Creating Instances of Struct Types
Adding Behavior to a Struct Type
Calling Struct Methods
Pointer Method Receivers
Type Composition
Overriding Methods of Embedded Type
Working with Interfaces
Concurrency
Goroutines
GOMAXPROCS and Parallelism
Channels
Unbuffered Channel
Buffered Channels
Summary
Chapter 4: Getting Started with Web Development
net/http Package
Processing HTTP Requests
Serve Mux
Handler
Building a Static Web Server
Creating Custom Handlers
Using Functions as Handlers
http.HandlerFunc type
ServeMux.HandleFunc Function
DefaultServeMux
http.Server Struct
Gorilla Mux
Building a RESTful API
Data Model and Data Store
Configuring the Multiplexer
Handler Functions for CRUD Operations
Summary
Chapter 5: Working with Go Templates
text/template Package
Working with text/template
Define Named Templates
Declaring Variables
Using Pipes
Building HTML Views Using html/template
Building a Web Application
Data Structure and Data Store
main function
Views and Template Definition Files
Initializing View Templates
Rendering the Index Page
Rendering the Add Page
Rendering the Edit Page
Summary
Chapter 6: HTTP Middleware
Introduction to HTTP Middleware
Writing HTTP Middleware
How to Write HTTP Middleware
Writing a Logging Middleware
Controlling the Flow of HTTP Middleware
Using Third-Party Middleware
Using Gorilla Handlers
Installing Gorilla Handlers
Working with Gorilla Handlers
Middleware Chaining with the Alice Package
Installing Alice
Working with Alice
Using Middleware with the Negroni Package
Getting Started with Negroni
Installing Negroni
Routing with Negroni
Registering Middleware
Registering Middleware for Specific Routes
Working with a Negroni Middleware Stack
Sharing Values Among Middleware
Using Gorilla context
Setting and Getting Values with Gorilla context
Summary
Chapter 7: Authentication to Web Apps
Authentication and Authorization
Authentication Approaches
Cookie-Based Authentication
Token-Based Authentication
Authentication with OAuth 2
Understanding OAuth 2
Authentication with OAuth 2 using the Goth Package
Authentication with JSON Web Token
Working with JWT Using the jwt-go Package
Generating the JWT Token
Validating the JWT Token
Running and Testing the API Server
Using HTTP Middleware to Validate JWT Tokens
Summary
Chapter 8: Persistence with MongoDB
Introduction to MongoDB
Getting Started Using MongoDB
Introduction to mgo Driver for MongoDB
Installing mgo
Connecting to MongoDB
Accessing Collections
CRUD Operations with MongoDB
Inserting Documents
Inserting Struct Values
Inserting Embedded Documents
Reading Documents
Retrieving All Records
Sorting Records
Retrieving a Single Record
Updating Documents
Deleting Documents
Indexes in MongoDB
Managing Sessions
Summary
Chapter 9: Building RESTful Services
RESTful APIs: the Backbone of Digital Transformation
API-Driven Development with RESTful APIs
Go: the Great Stack for RESTful Services
Go: the Great Stack for Microservice Architecture
RESTful Services in Microservice Architecture
Building RESTful APIs
Third-Party Packages
Application Structure
Data Model
Resource Modeling for RESTful APIs
Mapping Resources with Application Routes
Routes for the Users Resource
Routes for the Tasks Resource
Adding Route-Specific Middleware
Routes for the TaskNote Resource
Initializing Routes for a RESTful API
Setting up the RESTful API Application
Initializing Configuration Values
Loading Private/Public RSA Keys
Creating a MongoDB Session Object
Adding Indexes into MongoDB
Initialization Logic in the common Package
Starting the HTTP Server
Authentication
Generating and Verifying JWT
Generating JWT
Sending JWT to the Server
Authorizing JWT
Application Handlers
Helper for Displaying HTTP Errors
Handling Data for an HTTP Request Lifecycle
Handlers for the Users Resource
JSON Resource Models
Handlers for the Users Resource
Registering New Users
Logging in to the System
Data Persistence with MongoDB
Handlers for the Tasks Resource
JSON Resource Models
Handlers for the Tasks Resource
Testing API Operations for the Tasks Resource
Handlers for Notes Resource
JSON Resource Models
Go Dependencies Using Godep
Installing the godep Tool
Using godep with TaskManager
Restoring an Application’s Dependencies
Deploying HTTP Servers with Docker
Introduction to Docker
Writing Dockerfile
Go Web Frameworks
Summary
References
Chapter 10: Testing Go Applications
Unit Testing
Test-Driven Development (TDD)
Unit Testing with Go
Writing Unit Tests
Getting Test Coverage
Benchmark Unit Tests
Verifying Example Code
Skipping Test Cases
Running Tests Cases in Parallel
Putting Tests in Separate Packages
Testing Web Applications
Testing with ResponseRecorder
Testing with Server
BDD Testing in Go
Behavior-Driven Development (BDD)
Behavior-Driven Development with Ginkgo
Refactoring the HTTP API
Writing BDD-style Tests
Installing Ginkgo and Gomega
Bootstrapping a Suite
Adding Specs to the Suite
Organizing Specs with Containers
Writing Specs in the Test File
Running Specs
Summary
Chapter 11: Building Go Web Applications on Google Cloud
Introduction to Cloud Computing
Infrastructure as a Service (IaaS)
Platform as a Service (PaaS)
Container as a Service
Introduction to Google Cloud
Google App Engine (GAE)
Cloud Services with App Engine
User Authentication
Cloud Datastore
Cloud Bigtable
Google Cloud SQL
Memcache
Search
Traffic Splitting
Logging
Task Queues
Security Scanning
Google App Engine for Go
Go Development Environment
Building App Engine Applications
Writing an HTTP Server
Creating the Configuration File
Testing the Application in Development Server
Deploying App Engine Applications into the Cloud
Creating Hybrid Stand-alone/App Engine applications
Working with Cloud Native Databases
Introduction to Google Cloud Datastore
Entities
Ancestors and Descendants
Working with Cloud Datastore
Creating a New Entity
Querying the Datastore
Running the Application
Building Back-end APIs with Cloud Endpoints
Cloud Endpoints for Go
Installing the endpoints Package
Cloud Endpoints Back-end APIs in Go
Running Cloud Endpoints Back-end API
Generating Client Libraries
Summary
References
Index