Part 1. Go and web applications
Web applications are probably the most widely used type of software application today and if youâre connected to the internet, you would hardly pass a day without using one. Even if youâre mostly on a mobile device, you still are using web applications. Many mobile applications that look like native applications are hybrids that have portions that built on web technologies.
Knowing HTTP is the foundation to learning how to write web applications, so these first two chapters will introduce HTTP. I will also explain why using Go for writing web applications is a good idea. I will jump straight into showing you how to write a simple internet forum using Go and show you a birdâs-eye view of writing a web application.
Chapter 1. Go and web applications
This chapter covers
- Defining web applications
- Using Go to write web applications: the advantages
- Understanding the basics of web application programming
- Writing the simplest possible web application in Go
Web applications are ubiquitous. Take any application you use on a daily basis, and likely itâs a web application or has a web application variant (this includes mobile apps). Any language that supports software development that interfaces with human beings will inevitably support web application development. One of the first things developers of a new language do is build libraries and frameworks to interact with the internet and the World Wide Web. There are myriad web development tools for the more established languages.
Go is no different. Go is a relatively new programming language created to be simple and efficient for writing back end systems. It has an advanced set of features and focuses on programmer effectiveness and speed. Since its release, Go has gained tremendous popularity as a programming language for writing web applications and *-as-a-Service systems.
In this chapter, youâll learn why you should use Go for writing web applications and youâll learn all about web applications.
1.1. Using Go for web applications
So why should you use Go for writing web applications? My guess is, having bought this book, you have an inclination to find out the answer. Of course, as the author of a book that teaches Go web programming, I believe there are strong and compelling reasons to do so. As you continue reading this book, youâll get a sense of Goâs strengths in web application development and, I hope, agree with me about the usefulness of Go.
Go is a relatively new programming language, with a thriving and growing community. It is well suited for writing server-side programs that are fast. Itâs simple and familiar to most programmers who are used to procedural programming, but it also provides features of functional programming. It supports concurrency by default, has a modern packaging system, does garbage collection, and has an extensive and powerful set of built-in standard libraries.
Plenty of good-quality open source libraries are available that can supplement what the standard libraries donât have, but the standard libraries that come with Go are quite comprehensive and wide-ranging. This book sticks to the standard libraries as much as possible but will occasionally use third-party, open source libraries to show alternative and creative ways the open source community has come up with.
Go is rapidly gaining popularity as a web development language. Many companies, including infrastructure companies like Dropbox and SendGrid, technology-oriented companies such as Square and Hailo, as well as more traditional companies such as BBC and The New York Times, have already started using Go.
Go provides a viable alternative to existing languages and platforms for developing large-scale web applications. Large-scale web applications typically need to be
- Scalable
- Modular
- Maintainable
- High-performance
Letâs take a look at these attributes in detail.
1.1.1. Scalable web applications and Go
Large-scale web applications should be scalable. This means you should be able to quickly and easily increase the capacity of the application to take on a bigger volume of requests. The application should scale also linearly, meaning you should be able to add more hardware and process a corresponding number of requests.
We can look at scaling in two ways:
- Vertical scaling, or increasing the amount of CPUs or capacity in a single machine
- Horizontal scaling, or increasing the number of machines to expand capacity
Go scales well vertically with its excellent support for concurrent programming. A single Go web application with a single OS thread can be scheduled to run hundreds of thousands of goroutines with efficiency and performance.
Just like any other web applications, Go can scale well horizontally as well as by layering a proxy above a number of instances of a Go web app. Go web applications are compiled as static binaries, without any dynamic dependencies, and can be distributed to systems that donât have Go built in. This allows you to deploy Go web applications easily and consistently.
1.1.2. Modular web applications and Go
Large-scale web applications should be built with components that work interchangeably. This approach allows you to add, remove, or modify features easily and gives you the flexibility to meet the changing needs of the application. It also allows you to lower software development costs by reusing modular components.
Although Go is statically typed, it has an interface mechanism that describes behavior and allows dynamic typing. Functions can take interfaces, which means you can introduce new code into the system and still be able to use existing functions by implementing methods required by that interface. Also, with a function that takes an empty interface, you can put any value as the parameter because all types implement the empty interface. Go implements a number of features usually associated with functional programming, including function types, functions as values, and closures. These features allow you to build more modular code by providing the capability of building functions out of other functions.
Go is also often used to create microservices. In microservice architecture large-scale applications can be created by composing smaller independent services. These services are interchangeable and organized around capabilities (for example, a systems-level service like logging or an application-level service such as billing or risk analysis). By creating multiple small Go services and composing them into a single web application, you enable these capabilities to be swappable and therefore more modular.
1.1.3. Maintainable web applications and Go
Like any large and complex applications, having an easily maintainable codebase is important for large-scale web applications. Itâs important because large-scale applications often need to grow and evolve and therefore you need to revisit and change the code regularly. Complex, unwieldy code takes a long time to change and is fraught with risk of something breaking, so it makes sense to keep the source code well organized and maintainable.
Go was designed to encourage good software engineering practices. It has a clean and simple syntax thatâs very readable. Goâs package system is flexible and unambiguous, and thereâs a good set of tools to enhance the development experience and help programmers to write more readable code. An example is the Go sourc...