Go Web Programming
Author: Sau Sheong Chang
Review created: 2019-07-06
Go Web Programming is a rather complete book on how to do web programming in general using the Go language. What do I mean by that? I mean that the book describes rather thoroughly everything from the basics of HTTP (what does an HTTP request consist of), how to set up a Postgres database and connect to it, how to write a web application in Go and how to test it, and how to deploy the finished application. All this is done in less than 300 pages. Rather impressive! Did I mention that the book also includes a rather extensive chapter on concurrency in Go?
I bought the book because I needed to learn more about the HTML templates in Go. At the time I was writing the web application on which this review is published, and I struggled a bit with how to structure the template files and with some details in them. After having read the chapter on templates in Go Web Programming, many of the problems I had was resolved (the other problems I solved by googling and by trial and error).
The chapters in the book are:
- Go and web applications
- Go ChitChat
- Handling requests
- Processing requests
- Displaying content
- Storing data
- Go web services
- Testing your application
- Leveraging Go concurrency
- Deploying Go
As previously mentioned, the books starts off with a chapter that describes HTTP and web programming in general. The author seems a bit irritated that many programmers, at least in his opinion, don't seem to have much knowledge about that topic. The first chapter also contain some very brief introductory content on Go.
The next chapter is a whirlwind tour of how to write a full web application, including storing content in a DB, in Go. It acts as a teaser for the rest of the chapters. When reading the chapter you probably won't understand everything, but you will get a glimpse on what you can expect to have learned after having read the rest of the book. This is a good approach, I think.
The following chapters deals in more detail in how to write a web application in Go. It is extremely easy to create a basic web application in Go as a web server is included in the language and can be started with one line of code. So what writing a web application in Go is about is how to instruct that web server what URLs to handle and how to handle each request to the supported URLs. To specify that handlers and handler functions are written. That are what makes up the larger part of a web application written in Go. These kinds of things are described in the chapters Handling requests and Processing requests.
The chapter Displaying content deals with the template support in Go. It does a good job of describing what a template is, how one looks in Go and which features that are included in the Go template engine.
The chapter of storing data describes, well, how store data that can be used in a web application. A couple of different options are listed, the most useful one is to store the data in a database. To illustrate that option, the chapter contains instructions on how to connect to a Postgres database and perform CRUD operations against it. Quite useful if you're using a SQL database in your webapp.
The next chapter is on testing. Three different packages for doing this are described: the built in testing package and the third-party gocheck and Ginkgo packages. It also includes sections on mocking and testing HTTP applications. I liked this chapter a lot. I contains info that not only is relevant for web applications but for other applications as well. And it includes info about how to benchmark applications. Really useful.
The chapter on concurrency surprised me. How did the author manage to squeeze in such much material about this rather difficult topic in a book on web programming? That is rather impressive. The chapter would be a good overview of the concurrency related stuff in Go in any book specialized on that topic. In the end of the chapter, it is described how concurrency specifically can be used in web applications, which nicely weaves the chapter topic into the main topic of the book.
The last chapter of the book describes how Go applications can be deployed to that they can be accessed from the Internet. A number of different options are described:
- deploying to a stand-alone server
- deploying to a PaaS provider (Heroku)
- deploying to another PaaS provider (Google App Engine)
- deploying to Docker locally
- deploying to Docker on a cloud based host (Digital Ocean)
I think the book did a good job of describing the different options. I was good to have some options listed, although I miss an option about writing the web application using serverless techniques. The lack of such an option is not surprising though, considering that the book was written in 2016, when serverless technology was not nearly as developed as it is today.
Summary: I liked Go Web Programming a lot. I learned the stuff about Go HTML templates which was my original reason for buying the book. I also learned a lot of new things, e.g. about unit testing and I got a refresher about the concurrency constructs in Go. The book is easy to read and the author has an informal way of writing which I like. I would recommend this book to anyone interested in learning how to develop web applications in Go.