About this ebook
This book takes its time to go over every important aspect of backend programming, from the fundamentals to more advanced techniques, so that you can become experts in Go scripting and all the things Go can do. Quickly introducing readers to Go and its ecosystem, the book walks them through installing the language and creating a development environment with Visual Studio Code. Next, it takes a baby step into learning the basics of building web servers with the net/http package, going over topics like routing, handling various HTTP methods, and the structures of requests and responses. Path variables, regex-based routing, custom handlers, and middleware are some of the advanced routing topics covered, which uses the robust gorilla/mux package. After introducing session and cookie management, the book moves on to user authentication, covering topics such as OAuth2 integration, JWT for secure APIs, and more.
The book then teaches various aspects of database integration with GORM, covering topics such as connecting to SQL databases, performing CRUD operations, managing migrations, and handling transactions and concurrency control. The Gin framework for designing and implementing microservices, REST and gRPC for inter-service communication, and Kubernetes for containerizing applications are also covered in detail. Also covered is message brokering with Apache Kafka and NSQ for asynchronous systems, which guarantees resilient systems and efficient message delivery. Secure coding practices, HTTPS with crypto/tls, avoiding SQL injections and XSS attacks, and configuration management with Viper are also one of the main goal of the book.
Last but not least, the book covers testing and debugging with tools such as Delve, Testify, and GoMock. It then teaches readers through various deployment strategies, such as blue-green, canary, and rolling deployments with AWS CodeDeploy. Utilizing Go's robust features and clean scripting capabilities, this book provides you with the necessary knowledge and skills to develop secure, scalable, and resilient backend systems.
Key Learnings
- Use gorilla/mux to implement advanced routing techniques for flexible URL handling.
- Implement strong API security with user authentication using OAuth2 and JWT.
- Make use of GORM's advanced capabilities of migrations and transactions, to integrate SQL databases.
- Use Gin, Kubernetes, and gRPC to build and launch scalable microservices.
- Make use of NSQ and Kafka for asynchronous processing.
- Prevent frequent vulnerabilities of SQL injection and XSS attacks.
- Use Testify, GoMock, and Delve to streamline testing and debugging.
- Use AWS CodeDeploy with blue-green and canary deployment strategies to deploy applications.
Table of Content
- Understanding Go for Backend Development
- Building a Basic Web Server with net/http
- Advanced Routing with gorilla/mux
- User Authentication with Oauth2 and JWT
- Integrating Databases with GORM
- Creating Microservices in Go
- Message Brokering with NSQ and Apache Kafka
- Securing Go Applications
- Testing and Debugging Go Applications
- Deploying Go Applications
Related to Programming Backend with Go
Related ebooks
Programming Backend with Go: Build robust and scalable backends for your applications using the efficient and powerful tools of the Go ecosystem Rating: 0 out of 5 stars0 ratingsGo Programming Cookbook Rating: 0 out of 5 stars0 ratingsCORS Essentials Rating: 0 out of 5 stars0 ratingsWebRTC Cookbook Rating: 0 out of 5 stars0 ratingsGo Programming Cookbook: Over 75+ recipes to program microservices, networking, database and APIs using Golang Rating: 0 out of 5 stars0 ratingsGetting Started with Meteor.js JavaScript Framework - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Go Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsNode Web Development, Second Edition Rating: 0 out of 5 stars0 ratingsLearning Rust Rating: 0 out of 5 stars0 ratingsHarnessing the Power of Firebase and Google Cloud Platform: A Comprehensive Guide for Developers Rating: 0 out of 5 stars0 ratingsFull Stack Development Explained: From Frontend to Backend Rating: 0 out of 5 stars0 ratingsMastering Go: Navigating the World of Concurrent Programming Rating: 0 out of 5 stars0 ratingsMastering Google App Engine: Build robust and highly scalable web applications with Google App Engine Rating: 0 out of 5 stars0 ratingsReact Components Rating: 0 out of 5 stars0 ratingsInter-Service Communication with Go: Mastering protocols, queues, and event-driven architectures in Go (English Edition) Rating: 0 out of 5 stars0 ratingsMastering Go Network Automation: Automating Networks, Container Orchestration, Kubernetes with Puppet, Vegeta and Apache JMeter Rating: 0 out of 5 stars0 ratingsMastering Go Network Automation Rating: 0 out of 5 stars0 ratingsNode.js: Novice to Ninja Rating: 0 out of 5 stars0 ratingsCode with Java 21: A practical approach for building robust and efficient applications (English Edition) Rating: 0 out of 5 stars0 ratingsPractical C++ Backend Programming Rating: 0 out of 5 stars0 ratingsGolang for Jobseekers: Unleash the power of Go programming for career advancement (English Edition) Rating: 0 out of 5 stars0 ratingsIntroduction to Google's Go Programming Language: GoLang Rating: 0 out of 5 stars0 ratingsMastering Concurrent Programming with Go Rating: 0 out of 5 stars0 ratingsUltimate Nuxt.js for Full-Stack Web Applications Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5React Deep Dive Rating: 5 out of 5 stars5/5HTML, CSS, & JavaScript All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsLearn Python in 10 Minutes Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5The CSS Guide: The Complete Guide to Modern CSS Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Clean Code in JavaScript: Develop reliable, maintainable, and robust JavaScript Rating: 5 out of 5 stars5/5Deep Learning For Dummies Rating: 0 out of 5 stars0 ratingsBeginning C++ Game Programming Rating: 4 out of 5 stars4/5Modern C++ Programming Cookbook Rating: 5 out of 5 stars5/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5The Easiest Way to Learn Design Patterns Rating: 0 out of 5 stars0 ratingsLearn SQL in 24 Hours Rating: 5 out of 5 stars5/5Python for Finance Cookbook: Over 50 recipes for applying modern Python libraries to financial data analysis Rating: 0 out of 5 stars0 ratingsMastering JavaScript Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Artificial Intelligence with Python Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Narrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5
Reviews for Programming Backend with Go
0 ratings0 reviews
Book preview
Programming Backend with Go - Julian Braun
Programming Backend with Go
Build robust and scalable backends for your applications using the efficient and powerful tools of the Go ecosystem
Julian Braun
Prologue
You have arrived at Programming Backend with Go.
Hi! I'm Julian Braun, and I can't wait to show you all the cool stuff about Go backend development. Working with Go for many years has shown me how effective and efficient it is for building scalable, high-performance backend systems. I hope that this book will be useful to you whether you are an experienced developer or just getting your feet dipped in the world of backend programming. The Google Go language, also known as Golang, is known for its simplicity, efficiency, and dependability. When speed and scalability are of the utmost importance, it excels in backend development. We will build real-world apps and services that can handle the demands of modern web environments by exploring how to harness Go's full potential throughout this book.
To get things rolling, we'll setup our development environment and make sure you have everything you need to get things done. I'll show you how to set up Visual Studio Code, install Go, and use Go modules to manage dependencies. A well-configured environment can greatly enhance your productivity and development ease, so this foundational step must be taken for anyone interested in diving into Go programming. After everything is ready to go, we will learn using Go's net/http package to build web servers. The book will teach you the ins and outs of serving static files, responding to requests, and routing various URLs to their respective handlers. As a foundation for more advanced topics, these skills are the bedrock of web development.
In subsequent sections, we will learn more complex routing methods implemented within the gorilla/mux package. With this robust library, you can easily build sophisticated web applications with more expressive and flexible URL handling. Additionally, we will discuss error handling, custom handlers, and middleware to make sure your applications are strong and easy to maintain.
Extensive coverage of security will be provided, as it is an essential component of backend development. You will discover how to construct secure apps that safeguard users' and data's information, from incorporating user authentication with OAuth2 and JWT to protecting your apps from typical vulnerabilities such as SQL injection and XSS attacks.
Go stands out due to its top-notch concurrency and parallelism support. We'll make the most of this by creating and implementing microservices. You will discover how to create, launch, and oversee scalable microservices using tools like Gin and Kubernetes. In addition, we will go over REST and gRPC for inter-service communication, which will equip you to construct service-oriented architectures that are both efficient and cohesive.
The ability to efficiently manage data is fundamental to many applications, and GORM will teach you just that. The fundamentals of CRUD operations as well as more complex features like transactions, migrations, and concurrency control will all be covered. In addition, you will gain knowledge of message brokering with Apache Kafka and NSQ, which will equip you to create apps that can manage asynchronous workloads with high throughput. We will learn tools like Delve, Testify, and GoMock, which are essential for any developer, to help with testing and debugging. You'll learn how to write thorough tests, mock dependencies, and debug applications to ensure reliability and bug-freeness.
At last, we'll talk about deployment strategies, such as blue-green, canary, and rolling. By learning how to automate your deployment process with AWS CodeDeploy, you can keep your applications running smoothly and up-to-date at all times. More than simply a book, Programming Backend with Go
is an all-inclusive manual for turning you into an expert Go backend developer. The knowledge and abilities you acquire here will be very useful regardless of the complexity of the systems you're developing, be it simple web applications or complex distributed systems. Therefore, I say we jump right in and start making magic.
Copyright © 2024 by GitforGits
All rights reserved. This book is protected under copyright laws and no part of it may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without the prior written permission of the publisher. Any unauthorized reproduction, distribution, or transmission of this work may result in civil and criminal penalties and will be dealt with in the respective jurisdiction at anywhere in India, in accordance with the applicable copyright laws.
Published by: GitforGits
Publisher: Sonal Dhandre
www.gitforgits.com
Printed in India
First Printing: April 2024
Cover Design by: Kitten Publishing
For permission to use material from this book, please contact GitforGits at [email protected].
Content
Preface
GitforGits
Acknowledgement
Chapter 1: Understanding Go for Backend Development
Chapter Overview
Go and its Ecosystem
Core Features
Standard Library
Go Modules and Package Management
Popular Libraries for Backend Development
Messaging and Stream Processing
Security and Authentication
Containerization and Virtualization
Networking and Protocols
Testing and Deployment
Installing Go
Preparation and Download
Installation
Setting up the Environment
Verification
Setting up Visual Studio Code
Installing VS Code and Go Extension
Configuring Go Extension
Setting up the Workspace
Debugging Setup
Testing Integration
Go Modules for Package Management
What are Go Modules?
Setting up New Module
Adding Dependencies
Understanding go.mod File
Using go.sum File
Managing Upgrades and Downgrades
Basics of a Go program
Basic Structure of a Go Program
Variables and Types
Control Structures
If statement
For loop
Switch statement
Arrays, Slices, and Maps
Arrays
Slices
Maps
Functions
Concurrency with Goroutines
Introduction to ‘gitforgits.com’ Project
Project Overview
Functional Requirements
Technical Requirements
Design Considerations
Quick Project Code Structure
Git for Version Control
Why use Git?
Basic Git Commands
Adding Files and Committing Changes
Branching
Viewing Changes and Logs
Merging Branches
Handling Merge Conflicts
Using Git with Remote Repositories
Docker Basics
What are Docker Containers?
Images and Containers
Dockerfile
Building and Running Docker Containers
Docker Compose
Starting and Managing Project Containers
Volumes and Networks
Summary
Chapter 2: Building a Basic Web Server with net/http
Chapter Overview
Introduction to ‘net/http’ Package
Core Components of net/http
Working with HTTP Servers
Deep Dive into HTTP Handlers
Understanding Request and Response
Creating your first web server
Setup Basic HTTP Server
Handling Different Routes
Handling HTTP Methods
Query Parameters and URL Fragments
Running and Testing Server
Understanding Request and Response Structures
HTTP Request Structure
HTTP Response Structure
Flow of Request and Response
Client Request
Server Handling
Server Response
Client Receives
Handling of Requests and Responses
Basics of Routing with http.ServeMux
Understanding Routing
Introduction to http.ServeMux
Features of http.ServeMux
Sample Program: Routing with http.ServeMux
Handling Different HTTP Methods
HTTP Methods
GET
POST
PUT
DELETE
PATCH
HEAD
OPTIONS
TRACE
CONNECT
Sample Program: Implementing Handlers
Query Parameters and URL Fragments
Importance of Query Parameters and URL Fragments
Query Parameters
URL Fragments
Sample Program: Utilizing Query Parameters and URL Fragments
Handling Query Parameters
URL Fragments for Client-Side Handling
Testing Implementation
Serving Static Files and Assets
Types of Static Files and Assets
Serving Static Files
Structuring Static Directory
Testing Static File Serving
Structuring Go Web Application
Project Structure Overview
Directories Overview
Sample Program: Adopting Structure in gitforgits.com
Summary
Chapter 3: Advanced Routing with gorilla/mux
Chapter Overview
Introduction to gorilla/mux
Why Gorilla Mux?
Up and Running with Gorilla Mux
Advanced Routing Techniques
Regular Expression Constraints
Subdomain Routing
Query Parameter Routing
Path Variables
Defining Path Variables
Accessing Path Variables
Managing Path Variables
Regex-based Routing
Understanding Regex
Advantages of Regex-Based Routing
Implementing Regex-Based Routing
Testing Regex-Based Routing
Custom Handlers and Middleware
Purpose of Custom Handlers and Middleware
Creating Custom Handlers and Middleware in Gorilla Mux
Custom Handlers
Middleware
Sample Program: Manage Routing with Custom Handlers
Error Handling and HTTP Status Codes
Understanding HTTP Status Codes
Implementing Error Handling
Custom Error Handler
Using Middleware for Error Handling
Sample Program: Handling Specific Errors
Perform RESTful Routing
Understanding RESTful Routing
Implementing RESTful Routing using Gorilla Mux
Setting up Gorilla Mux Router
Defining Handler Functions
Building CRUD Operations
Setting up the Environment
Defining Book Model
Building Handlers
Create (POST)
Read (GET)
Update (PUT)
Delete (DELETE)
Assembling the Router and Starting the Server
Summary
Chapter 4: User Authentication with Oauth2 and JWT
Chapter Overview
Basics of User Authentication
What is User Authentication?
Working of User Authentication
Credential Verification
Session Creation and Management
Using Authentication to Protect Routes
Using OAuth2 with Go
Setting up OAuth2
Register the Application
Setting up OAuth2 Client
Flows in OAuth2
Integration with Social Media Logins
Integration with Google Login
Integration with Facebook Login
Setting up Facebook OAuth Configuration
Handling Facebook Callback
Integration with Email Login
Implementing JWT for Secure APIs
Advantages of JWT for Secure APIs
Implementing JWT
Install the JWT Go Package
Create the JWT Token
Setup HTTP Endpoint to use JWT
Create Middleware to Protect Routes
Sessions and Cookie Management
Understanding Sessions and Cookies
Managing Cookie-Based User Sessions
Secure Session Management Techniques
User registration and Password Management
Creating System for User Registration and Password Mgmt
User Registration API
Managing and Protecting Passwords
Defining Password Policies
Rate Limiting and Abuse Prevention
Understanding Rate Limiting
Implementing Custom Rate Limiting
Define Rate Limiter Structure
Integrating Rate Limiting
Protecting against DDoS Attacks
Summary
Chapter 5: Integrating Databases with GORM
Chapter Overview
Setting up GORM for ORM
Introduction to GORM
Setting up GORM in Development Environment
Install GORM
Connect to the Database
Define Models
Perform Migrations
Database Selection for Application
Common Databases used in Go Apps
Choosing PostgreSQL
Designing Database Schema
Integrating PostgreSQL with GORM
Database CRUD Operations
Setting up the Environment
CRUD Operations in GORM
Create (Inserting Data)
Read (Querying Data)
Update (Modifying Data)
Delete (Removing Data)
Advanced GORM Features
Understanding Hooks in GORM
Commonly Used GORM Hooks
Sample Program: Implementing a Hook
Understanding Scopes in GORM
Putting Hooks and Scopes into Practice
Auto-Logging Changes
Advanced Scopes for Complex Queries
Managing Database Migrations
Understanding Database Migrations
GORM's Migration Features
Sample Program: Migration with GORM
Initial Setup
Adding New Model with Relationships
Modifying Existing Schema
Transactions and Concurrency Control
Understanding Transactions
Using GORM to Manage Transactions
Concurrency Control
Managing Concurrency
Summary
Chapter 6: Creating Microservices in Go
Chapter Overview
Design Principles for Microservices
Decomposition by Business Capability
Independence and Decentralization
Failure Isolation
Automate Everything
Observability
Setting up Microservices Environment
Introduction to Gin Framework
Capabilities of Gin
Setting up Gin for gotforgits.com
Install Gin
Create a Basic Gin Server
Test Your Setup
Inter-service Communication with REST and gRPC
Overview
Choosing Communication Protocols: REST vs. gRPC
REST for Inter-service Communication
gRPC for Inter-service Communication
Setting up REST and gRPC
Install Necessary Packages
Creating ProductService with REST
Creating ProductService with gRPC
Creating ClientService
Conclusion
Using gRPC and Protocol Buffers
Introduction to Protocol Buffers and gRPC
What are Protocol Buffers?
Why gRPC?
Service Interfaces and Message Structures with Protocol Buffers
Install Protocol Buffers Compiler
Define Proto File
Compile Proto File
How gRPC utilizes Protocol Buffers?
Implementing Service
Creating the Client
Implementing API Gateways
Introduction to API Gateways
Significance of an API Gateway
Implementing an API Gateway
Install Kong
Configure Service Routing
Handling Failures
Implementing Aggregations
Authentication and Authorization in Microservices
Introduction to Authentication and Authorization
Strategies for Securing Microservices
Centralized Identity and Access Management
Using API Gateways for Security
Implementing Authentication and Authorization
Setup Authentication at the API Gateway
Microservice Authorization
Containerize Microservices with Kubernetes
Introduction to Kubernetes and Containerization
Setting up Kubernetes
Install Minikube and kubectl
Start Minikube
Understanding Containerization
Install Docker
Containerizing Microservices using Gin
Create a Dockerfile
Build and Run Your Docker Container
Deploying to Kubernetes
Summary
Chapter 7: Message Brokering with NSQ and Apache Kafka
Chapter Overview
Introduction to Message Brokering
Concept of Message Brokering
What is a Message Broker?
Benefits of Asynchronous Communication
Setting up NSQ
Key Features of NSQ
Setting up NSQ in Development Environment
Install NSQ
Run ‘nsqlookupd’
Run ‘nsqd’
Run ‘nsqadmin’
Testing NSQ Setup
Integrating Apache Kafka with Go
Installing Apache Kafka
Integrating Kafka with Go
Install Kafka Go Client Library
Writing a Kafka Producer
Writing a Kafka Consumer
Handling Message Patterns
Understanding Pub/Sub and Queues
Implementing Pub/Sub and Queues
Implementing Pub/Sub with Kafka
Implementing Queues with NSQ
Ensuring Message Delivery and System Resilience
Key Concepts
Message Delivery Guarantees
System Resilience
Implementation Strategies
Scenario 1: Ensuring At-Least-Once Delivery in Kafka
Scenario 2: Handling Failures and Redundancies in NSQ
Scaling and Managing Brokers
Scaling NSQ
Scaling Kafka Horizontally
Adding More Brokers
Partition Rebalancing
Replication Factor
Monitoring and Managing Performance
Summary
Chapter 8: Securing Go Applications
Chapter Overview
Secure Coding Practices
Source Code and Binaries Checks
Source Code Analysis
Binaries Analysis
Updating Dependencies and Go
Fuzz Testing
Race Detection
Using Go's Vet Command
Using HTTPS with Crypto/TLS
Understanding crypto/tls Package
Setting up HTTPS Server with TLS
Generate TLS Certificates
Configure HTTPS Server
Test HTTPS Server
Securing Headers and Cookies
Configuring HTTP Headers Securely
Content-Security-Policy (CSP)
X-Content-Type-Options
X-Frame-Options
Strict-Transport-Security
Securely Configuring Cookies
Sample Program: Configuring Headers and Cookies
Preventing SQL Injections and XSS Attacks
Prevent SQL Injections
Use Prepared Statements (Parameterized Queries)
Escaping All User Inputs
Prevent XSS Attacks
Encoding and Escaping User Input
Use Content Security Policy (CSP)
OAuth2 and JWT for Secure Data Access
Implementing OAuth2
Implementing JWT for Secure Data Access
Summary
Chapter 9: Testing and Debugging Go Applications
Chapter Overview
Unit Testing with Testify
What is Testify?
Installing Testify
Writing First Test with Testify
Define Book Service
Setup Test Files and Write Tests
Running Tests
Mocking Dependencies with GoMock
What is GoMock?
Installing GoMock
Setting up GoMock for Project
Writing Tests using GoMock
Profiling Go Apps using ‘pprof’
What is pprof?
Setting up Profiling
Integrate pprof with Go Application
Running Your Application
Accessing Profiling Data
Analyzing Profile Data
Run Logging with Zerolog
Installing Zerolog
Setting up Zerolog
Sample Program: Logging using Zerolog
Debugging and Troubleshooting with Delve
Installing Delve
Sample Program: Debugging Scenarios
Scenario