Recently I saw an interesting post on Reddit: “I would like to be more full-stack,” user Fenugurod said. “I was studying Tailwindcsss and I’m pretty sure I can create really nice UIs with it. But what do you guys think about web development with Go? Most of my friends simply say to embrace the JS ecosystem with Nuxt or Next and use Go simply as an API.”
Here at Boot.dev, I’m biased. I love Go for nearly anything. It has so many great points – its concurrency, the goroutines, the compilation speed. But I also believe in using the right tool for the right job. The web development Go combo may be workable, but is it ideal?
The developers of Go certainly think so, seeing as they’ve published a whole blog post on the Web development Go combo. In their words, “Go delivers speed, security, and developer-friendly tools for Web Applications.”
🔗 Brief definitions:
When I say web development, I’m referring to the process of creating and maintaining websites and web applications that are accessible through the internet. It involves a combination of programming, design, and technical skills to build functional and visually appealing websites that cater to the needs of users.
Go, also informally known as Golang, is a statically typed, compiled language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It’s beloved by programmers for its simplicity, efficiency, and strong support for concurrent programming.
🔗 Web Dev in Go? A short answer
There are also projects like WASM (WebAssembly) that allow Go to run in your browser, bridging the gap between Go and traditional front-end technologies. It’s worth mentioning that WASM, released for the first time in March 2017, is still in its early stages and might not be suitable for all projects.
In conclusion, if you want to become a full-stack developer with Go, it’s totally possible. Go can power your backend, and with the right tools and frameworks, you can also integrate it with popular front-end technologies.
If you’re in for the longer answer, let’s ride on down.
🔗 Background of Go
First, let’s go back a bit to understand the background of Go. Basically, its simplicity, efficiency, and robust standard library provide developers with the tools you need to build scalable and maintainable web applications, along with enough safety tools for the British Government to use it. (““In particular, Go’s concurrency model makes it absurdly easy to build performant I/O-bound applications.” – Gov.uk’s web developers).
Go, often referred to as Golang, was conceived in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson, all at Google, and was released to an eager public in November 2009. These men were shooting for a language without the unnecessary complexities of languages like C++ while maintaining their strengths. They wanted a system programming language that was modern, concise, and fast both in execution and compilation.
Go was designed with two core principles in mind: simplicity and productivity. Its syntax is clean, which makes it easy to read and write. This simplicity also reduces the room for errors, leading to more robust code. With features like a rich standard library and a straightforward approach to concurrency, Go makes developers more productive. The language’s design also reduces the need for extensive debugging, which as any developer will tell you, also increases productivity.
What makes Go so good? Five key features separate it from the pack:
- Concurrency: Goroutines (lightweight threads managed by the Go runtime) and channels (a mechanism to safely communicate between goroutines) let developers write efficient, readable concurrent code.
- Garbage Collection: An automatic garbage collector reclaims memory used by data structures that are no longer accessible.
- Strong Typing: Go is a statically-typed language. The data type of a variable is checked at compile-time. This feature helps catch potential errors early in the development process.
- Compilation Speed: Go is known for its fast compilation times. This rapid feedback loop allows for quicker iterations and testing.
- Built-in HTML templating and HTTP routing in the stdlib: Go’s standard library comes with built-in packages for HTML templating (html/template) and HTTP routing (net/http). This makes it relatively straightforward to set up web servers and render dynamic content without relying on third-party libraries.
🔗 Go in Web Development
How does that all tie into web development? Let’s look over those features with an eye for Web development specifically.
🔗 Building web servers
One of Go’s strengths is its rich standard library, which I mentioned earlier. One package in particular is suitable for web dev: the net/http package. This package gives you a set of robust tools to quickly build HTTP servers. With just a few lines of code, you can set up servers, define routes, and handle requests. For simpler applications, you might not even need an external web framework.
Another strength is that concurrency I keep harping on about. Web servers often need to handle multiple requests simultaneously – for example, a user might be uploading a file while another is querying data, and yet another is logging in. In many traditional languages, handling these simultaneous tasks can be cumbersome, often requiring multi-threading or complex asynchronous programming models.
Go, however, was designed with this exact problem in mind. Its concurrency model is built around goroutines, which are much lighter than traditional threads and can be spawned in the thousands or even millions without significant overhead.
And then you can look beyond the vanilla packages for a golang framework or a third-party package that further simplifies web development or web application development:
- Gin: Offers a martini-like API but with much better performance. It’s known for its speed and small memory footprint. Comes with middleware support, routing, and error handling. (This one was most commonly mentioned in the /golang subreddit)
- Echo: Another high-performance, extensible framework. Echo focuses on performance and provides features like middleware support and data binding. Its API is designed to be simple and straightforward.
- Buffalo: An ecosystem for rapidly developing web applications in Go. It aims to be a one-stop-shop for Go web development.
- Gorilla Mux: While not a full-fledged framework, Gorilla Mux is a powerful URL router and dispatcher. It’s a part of the Gorilla web toolkit, which provides a range of tools and libraries for building web applications.
These Golang web frameworks are functional and simple.
🔗 What about the other possibilities?
Go is a statically-typed, compiled programming language, which generally means superior performance in terms of raw execution speed compared to interpreted languages like Python. Its concurrency further boosts its performance in scenarios like web servers where handling multiple simultaneous connections is crucial.
Python wins the award for beginners at the “hello world” stage. It is beloved for its simplicity and readability. But it is often slower than Go, especially in CPU-bound tasks. However, with frameworks like Django and FastAPI, Python remains a popular choice for web development, especially for applications where development speed is more critical than raw performance.
🔗 Pros of Doing Golang Web Development
Let’s break down the pros. The main pros? Speed, concurrency, and built-in HTML templating. I won’t spend too long since we’ve covered these above, bar the HTML section.
- Speed: Go’s speed and efficiency contribute to faster development cycles and high-performance web applications.
- Concurrency: Go’s concurrency model benefits web development, especially for handling multiple requests simultaneously
Go’s built in HTML templating, speed, and HTTP routing makes it a perfect match if you’re interested in new web development tools like HTMX. Plus, Go’s standard library includes the html/template package, which lets you easily render dynamic HTML content without needing any third-party libraries. It also automatically handles common security issues like cross-site scripting (XSS) by safely escaping strings by default.
It’s also worth highlighting that, no surprise, Go plays well with the Google suite of products and services, which are prevalent in the development space. For instance, the App Engine is a fully managed platform on Google Cloud that allows you to deploy applications without managing the underlying infrastructure. The standard environment is optimized for applications with fluctuating traffic. It has built-in support for Go. You can write your Go web application, define your web app’s configuration in an app.yaml file, and deploy it directly to App Engine. Automatic scaling, versioning, and zero server management for your web application, all effortless.
There’s one final reason it makes sense to use a backend language like Go for full-stack web development: an overall simpler ecosystem. I loved how Reddit user Markuswustenberg put it in their answer to Fenugurod’s question above:
“I’ve really embraced writing classic server-side-rendered UIs again. The simplicity is refreshing, and especially on smaller projects, where there’s no dedicated backend and frontend team, it’s nice to be able to just work on one codebase (in Go) and not write all the API glue code, handle network error cases, etc. Also, there’s no context switch, and you don’t have to keep up with the whole JS ecosystem, which I think is nice.”
🔗 Cons of Go for Web Dev
Now let’s cover a few of the most common obstacles in Go for web development.
Some folks might say that Go is tough to pick up. This might be true for novices, or for folks who are new to static typing because they’ve come from Python, which uses dynamic typing. But it’s an incredibly simple language. Personally, I’ll add that even if you’re new to it, the documentation is clear and easy to comprehend, and the community of Gophers is always happy to lend a helping hand to a newbie.
🔗 It’s your choice
In sum, Go is a great pick for you if you’re a web developer, at least in my eyes. It’s fast, it’s efficient, it’s concurrent, and it has a lot of useful, built-in functionality. If you’re a Golang developer already, it’s totally feasible to use it for full-stack development. And if not, there are still enough benefits to using Go for web development that it might be worth a try regardless.
The only time I would recommend not using Go for web development would be if you’re working on a project that heavily relies on the vast ecosystem of a specific language or framework, or if your team is deeply entrenched in another language and the switch would be too disruptive. Ultimately, the best tool depends on the project’s needs and the team’s expertise.
Otherwise? You’ve got nothing to lose from learning how to do web development with Go. Have fun!