You are previewing Web Development with Go: Building Scalable Web Apps and RESTful Services.
O'Reilly logo
Web Development with Go: Building Scalable Web Apps and RESTful Services

Book Description

Take a deep dive into web development using the Go programming language to build web apps and RESTful services to create reliable and efficient software. Web Development with Go provides Go language fundamentals and then moves on to advanced web development concepts and successful deployment of Go web apps to the cloud.

Web Development with Go will teach you how to develop scalable real-world web apps, RESTful services, and backend systems with Go. The book starts off by covering Go programming language fundamentals as a prerequisite for web development. After a thorough understanding of the basics, the book delves into web development using the built-in package, net/http. With each chapter you’ll be introduced to new concepts for gradually building a real-world web system.

The book further shows you how to integrate Go with other technologies. For example, it provides an overview of using MongoDB as a means of persistent storage, and provides an end-to-end REST API sample as well. Developers looking for a full-fledged web development framework for building web apps will be introduced to Beego. The book then moves on to demonstrate how to deploy web apps to the cloud using the Google Cloud platform. Finally, the book introduces Docker, a revolutionary container technology platform for deploying containerized Go web apps to the cloud.

Web Development with Go provides:

  • • Fundamentals for building real-world web apps in Go
  • • Through coverage of prerequisites and practical code examples
  • • Demo web apps for attaining a deeper understanding of web development
  • A reference REST API app which can be used to build scalable real-world backend services in Go
  • • A through demonstration of deploying web apps to the Cloud using the Google Cloud platform, and Docker for deploying Go servers
  • Go is a high-performance language while providing greater level of developer productivity, therefore Web Development with Go equips you with the necessary skills and knowledge required for effectively building robust and efficient web apps by leveraging the features of Go.

    Table of Contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Author
    8. About the Technical Reviewer
    9. Introduction
    10. Chapter 1 : Getting Started with Go
      1. Introducing Go
        1. Minimalistic Language with Pragmatic Design
        2. A Static Type Language with High Productivity
        3. Concurrency Is a Built-In Feature at the Language Level
        4. Go Compiles Programs Quickly
        5. Go as a General-Purpose Language
      2. Go Ecosystem
      3. Installing the Go Tools
        1. Checking the Installation
      4. Setting up a Work Environment
        1. Go Workspace
        2. GOPATH Environment Variable
        3. Code Organization Paths
      5. Writing Go Programs
        1. Writing a Hello World Program
        2. Writing a Library
        3. Testing Go Code
        4. Using Go Playground
      6. Using Go Mobile
      7. Go as a Language for Web and Microservices
      8. Summary
    11. Chapter 2 : Go Fundamentals
      1. Packages
        1. Package main
        2. Package Alias
        3. Function init
        4. Using a Blank Identifier
        5. Importing Packages
        6. Install Third-Party Packages
        7. Writing Packages
      2. Go Tool
        1. Formatting Go Code
        2. Go Documentation
      3. Working with Collections
        1. Arrays
        2. Slices
        3. Maps
      4. Defer, Panic, and Recover
        1. Defer
        2. Panic
        3. Recover
      5. Error Handling
      6. Summary
    12. Chapter 3 : User-Defined Types and Concurrency
      1. User-defined Types with Structs
        1. Creating a Struct Type
        2. Creating Instances of Struct Types
        3. Adding Behavior to a Struct Type
      2. Type Composition
      3. Overriding Methods of Embedded Type
      4. Working with Interfaces
      5. Concurrency
        1. Goroutines
        2. GOMAXPROCS and Parallelism
        3. Channels
      6. Summary
    13. Chapter 4 : Getting Started with Web Development
      1. net/http Package
      2. Processing HTTP Requests
        1. ServeMux
        2. Handler
      3. Building a Static Web Server
      4. Creating Custom Handlers
      5. Using Functions as Handlers
        1. http.HandlerFunc type
        2. ServeMux.HandleFunc Function
      6. DefaultServeMux
      7. http.Server Struct
      8. Gorilla Mux
      9. Building a RESTful API
        1. Data Model and Data Store
        2. Configuring the Multiplexer
        3. Handler Functions for CRUD Operations
      10. Summary
    14. Chapter 5 : Working with Go Templates
      1. text/template Package
        1. Working with text/template
        2. Define Named Templates
        3. Declaring Variables
        4. Using Pipes
      2. Building HTML Views Using html/template
      3. Building a Web Application
      4. Summary
    15. Chapter 6 : HTTP Middleware
      1. Introduction to HTTP Middleware
      2. Writing HTTP Middleware
        1. How to Write HTTP Middleware
        2. Writing a Logging Middleware
      3. Controlling the Flow of HTTP Middleware
      4. Using Third-Party Middleware
        1. Using Gorilla Handlers
        2. Middleware Chaining with the Alice Package
      5. Using Middleware with the Negroni Package
        1. Getting Started with Negroni
        2. Working with a Negroni Middleware Stack
      6. Sharing Values Among Middleware
        1. Using Gorilla context
        2. Setting and Getting Values with Gorilla context
      7. Summary
    16. Chapter 7 : Authentication to Web Apps
      1. Authentication and Authorization
      2. Authentication Approaches
        1. Cookie-Based Authentication
        2. Token-Based Authentication
      3. Authentication with OAuth 2
        1. Understanding OAuth 2
        2. Authentication with OAuth 2 using the Goth Package
      4. Authentication with JSON Web Token
        1. Working with JWT Using the jwt-go Package
        2. Using HTTP Middleware to Validate JWT Tokens
      5. Summary
    17. Chapter 8 : Persistence with MongoDB
      1. Introduction to MongoDB
      2. Getting Started Using MongoDB
        1. Introduction to mgo Driver for MongoDB
        2. Accessing Collections
      3. CRUD Operations with MongoDB
        1. Inserting Documents
        2. Reading Documents
        3. Updating Documents
        4. Deleting Documents
      4. Indexes in MongoDB
      5. Managing Sessions
      6. Summary
    18. Chapter 9 : Building RESTful Services
      1. RESTful APIs: the Backbone of Digital Transformation
        1. API-Driven Development with RESTful APIs
        2. Go: the Great Stack for RESTful Services
        3. Go: the Great Stack for Microservice Architecture
      2. Building RESTful APIs
        1. Third-Party Packages
        2. Application Structure
        3. Data Model
        4. Resource Modeling for RESTful APIs
        5. Adding Route-Specific Middleware
        6. Setting up the RESTful API Application
        7. Authentication
        8. Application Handlers
        9. JSON Resource Models
        10. Handlers for the Users Resource
        11. Registering New Users
        12. Logging in to the System
        13. Data Persistence with MongoDB
        14. JSON Resource Models
        15. Handlers for the Tasks Resource
        16. Testing API Operations for the Tasks Resource
        17. JSON Resource Models
      3. Go Dependencies Using Godep
        1. Installing the godep Tool
        2. Using godep with TaskManager
        3. Restoring an Application’s Dependencies
      4. Deploying HTTP Servers with Docker
        1. Introduction to Docker
        2. Writing Dockerfile
      5. Go Web Frameworks
      6. Summary
      7. References
    19. Chapter 10 : Testing Go Applications
      1. Unit Testing
      2. Test-Driven Development (TDD)
      3. Unit Testing with Go
        1. Writing Unit Tests
        2. Getting Test Coverage
        3. Skipping Test Cases
        4. Running Tests Cases in Parallel
        5. Putting Tests in Separate Packages
      4. Testing Web Applications
        1. Testing with ResponseRecorder
        2. Testing with Server
      5. BDD Testing in Go
        1. Behavior-Driven Development (BDD)
        2. Behavior-Driven Development with Ginkgo
      6. Summary
    20. Chapter 11 : Building Go Web Applications on Google Cloud
      1. Introduction to Cloud Computing
        1. Infrastructure as a Service (IaaS)
        2. Platform as a Service (PaaS)
        3. Container as a Service
      2. Introduction to Google Cloud
      3. Google App Engine (GAE)
        1. Cloud Services with App Engine
        2. Google App Engine for Go
        3. Go Development Environment
      4. Building App Engine Applications
        1. Writing an HTTP Server
        2. Creating the Configuration File
        3. Testing the Application in Development Server
        4. Deploying App Engine Applications into the Cloud
      5. Creating Hybrid Stand-alone/App Engine applications
      6. Working with Cloud Native Databases
        1. Introduction to Google Cloud Datastore
        2. Working with Cloud Datastore
      7. Building Back-end APIs with Cloud Endpoints
        1. Cloud Endpoints for Go
        2. Cloud Endpoints Back-end APIs in Go
      8. Summary
      9. References
    21. Index