Front Matter
Lets Go teaches you step-by-step how to create fast, secure and maintainable web applications using the fantastic programming language Go.
The idea behind this book is to help you learn by doing. Together well walk through the start-to-finish build of a web application from structuring your workspace, through to session management, authenticating users, securing your server and testing your application.
Building a complete web application in this way has several benefits. It helps put the things youre learning into context, it demonstrates how different parts of your codebase link together, and it forces us to work through the edge-cases and difficulties that come up when writing software in real-life. In essence, youll learn more than you would by just reading Gos (great) documentation or standalone blog posts.
By the end of the book youll have the understanding and confidence to build your own production-ready web applications with Go.
Although you can read this book cover-to-cover, its designed specifically so you can follow along with the project build yourself.
Break out your text editor, and happy coding!
Alex
Contents
Chapter 1.
Introduction
In this book well be building a web application called Snippetbox, which lets people paste and share snippets of text a bit like Pastebin or GitHubs Gists. Towards the end of the build it will look a bit like this:
Our application will start off super simple, with just one web page. Then with each chapter well build it up step-by-step until a user is able save and view snippets via the app. This will take us through topics like how to structure a project, routing requests, working with a database, processing forms and displaying dynamic data safely.
Then later in the book well add user accounts, and restrict the application so that only registered users can create snippets. This will take us through more advanced topics like configuring a HTTPS server, session management, user authentication and middleware.
Conventions
Throughout this book code blocks are shown with a silver background like below. If the code is particularly long, parts that arent relevant may be replaced with an ellipsis. To make it easy to follow along, most code blocks also have a title bar at the top indicating the name of the file that were working on.
File: hello.go package main ... // Indicates that some existing code has been omitted. func sayHello ( ) { fmt . Println ( "Hello world!" ) }
Terminal (command line) instructions are shown with a black background and start with a dollar symbol. These commands should work on any Unix-based operating system, including Mac OSX and Linux. Sample output is shown in silver beneath the command, like so:
$ echo "Hello world!"
Hello world!If youre using Windows, you should replace the command with the DOS equivalent or carry out the action via the normal Windows GUI.
Some chapters in this book end with an additional information section. These sections contain information that isnt relevant to our application build, but is still important (or sometimes, just interesting) to know about. If youre very new to Go, you might want to skip these parts and circle back to them later.
Hint: If youre following along with the application build I recommend using the HTML version of this book instead of the PDF or EPUB. The HTML version works in all browsers, and the proper formatting of code blocks is retained if you want to copy-and-paste code directly from the book.
About the author
Hey, Im Alex Edwards, a full-stack web developer and author. I live near Innsbruck, Austria.
Ive been working with Go for over 8 years, building production applications for myself and commercial clients, and helping people all around the world improve their Go skills.
You can see more of my writing on my blog (where I publish detailed tutorials), some of my open-source work on GitHub, and you can also follow me on Instagram and Twitter.
Copyright and disclaimer
Lets Go: Learn to build professional web applications with Go. Copyright 2022 Alex Edwards.
Last updated 2022-05-06 17:12:23 UTC. Version 2.18.2.
The Go gopher was designed by Renee French and is used under the Creative Commons 3.0 Attributions license. Cover gopher adapted from vectors by Egon Elbre.
The information provided within this book is for general informational purposes only. While the author and publisher have made every effort to ensure the accuracy of the information within this book was correct at time of publication there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this book for any purpose. Any use of this information is at your own risk.
Chapter 1.1.
Prerequisites
Background knowledge
This book is designed for people who are new to Go, but youll probably find it more enjoyable if you have a general understanding of Gos syntax first. If you find yourself struggling with the syntax, the Little Book of Go by Karl Seguin is a fantastic tutorial, or if you want something more interactive I recommend running through the Tour of Go.
Ive also assumed that youve got a (very) basic understanding of HTML/CSS and SQL, and some familiarity with using your terminal (or command line for Windows users). If youve built a web application in any other language before whether its Ruby, Python, PHP or C# then this book should be a good fit for you.
Go 1.18
The information in this book is correct for the latest major release of Go (version 1.18), and you should install this if youd like to code-along with the application build.
If youve already got Go installed, you can check the version number from your terminal by using the go version
command. The output should look similar to this:
$ go version
go version go1.18 linux/amd64If you need to upgrade your version of Go or install Go from scratch then please go ahead and do that now. Detailed instructions for different operating systems can be found here:
- Removing an old version of Go
- Installing Go on Mac OS X
- Installing Go on Windows
- Installing Go on Linux
Other software
There are a few other bits of software that you should make sure are available on your computer if you want to follow along fully. They are:
The curl tool for working with HTTP requests and responses from your terminal. On MacOS and Linux machines it should be pre-installed or available in your software repositories. Otherwise, you can download the latest version from here.
A web browser with good developer tools. Ill be using Firefox in this book, but Chromium, Chrome or Microsoft Edge will work too.
Your favorite text editor
Chapter 2.
Foundations
Alright, lets get started! In this first section of the book were going to lay the groundwork for our project and explain the main principles that you need to know for the rest of the application build.
Youll learn how to:
- which follows the Go conventions.
- and listen for incoming HTTP requests.