
Developing web applications with Golang can be a breeze, especially with the right tools and techniques. You can create a robust and scalable web server using the built-in http package.
Error handling is a crucial aspect of web development, and Golang provides a simple and efficient way to handle errors using the error type and the defer statement.
Logging is also essential for debugging and monitoring your web application, and Golang offers a variety of logging libraries, including the popular logrus library.
You might enjoy: Golang for Web Development
Error Handling
Error handling is a crucial aspect of any programming language, and Go is no exception. You can wrap an error in Go using the `errors.New()` function followed by `fmt.Errorf()` with the `%w` verb.
The wrapped error can be unwrapped using the `errors.Unwrap()` function. This allows you to add additional context to an error message while still preserving the original error.
Let's take a look at how this works in practice. In Example 1, we see how to wrap an error using `fmt.Errorf()` and the `%w` verb. The output shows that the error message includes additional context, such as "it's odd".
Discover more: Golang Go
However, as we can see in Example 2, unwrapping an error is necessary when checking if the error is of a specific type. The `errors.Unwrap()` function allows us to access the original error, enabling us to compare it to the expected error.
In Example 3, we see how to use `errors.Unwrap()` to check the contents of a wrapped error. This is a powerful tool for error handling, and it's essential to understand how to use it effectively.
Here are some common HTTP status codes and their corresponding error types:
By using these error types and HTTP status codes, you can simplify your error handling code and make it more robust.
Suggestion: Golang Create Error
Default and Limitations
Setting a default status can be a big improvement to our setup, but it's not without its drawbacks. We can't just ignore the problem of a default status of 0, which isn't ideal.
One of the main caveats of this approach is that it's non-standard, which means there's a cost in verbosity to be paid. This can be a major drawback for some developers.
Intriguing read: T Golang
However, there are ways to mitigate this issue, such as using a helper function like apperr.HTTPStatus() to return a default status of 500 / Internal Server Error.
Here are some of the key limitations of this approach:
- Non-standard
- Two ways to send responses to the client
- Makes certain behaviors implicit
These limitations can be a bit frustrating, but they're worth considering when deciding whether to use this approach.
Set Default
Setting a default status is a big improvement to our setup. It helps prevent errors like trying to serve an HTTP response with a status of 0.
We can add a helper function to our apperr package to solve this problem. This function can also be used from other places.
Our custom handler can be simplified and improved with this new function. We can omit the call to apierr.WithStatus when we want the default status of 500 / Internal Server Error.
Extending our approach to things like authorization is a natural next step.
Related reading: Golang Function Type
Other Limitations
This solution has its limitations, and it's essential to acknowledge them.

It doesn't provide an ergonomic way to specify a non-200, non-error status, such as 201, so you'll still need to fall back on w.WriteHeader().
Some applications might benefit from a function signature that returns a response along with an error, like func(*http.Request) (any, error), but this isn't provided.
Here are some specific limitations to consider:
- It doesn’t provide any ergonomic way to specify a non-200, non-error status (such as 201). For this, you still must fall back on w.WriteHeader().
- Some applications would be better served with a function signature such as func(*http.Request) (any, error), such that a response (likely to be rendered as JSON) is the first return argument.
Context and Logging
Context and logging are essential components of any Go application. We can use the context package in the standard library to manage request-scoped data.
The context package allows us to add logging to our application. We can create a function that takes a http.Handler and wraps it, returning a new handler that logs information about the request before invoking the wrapped handler. This works just as expected, logging the request information in our terminal.
To log the response as well, we need to intercept the WriteHeader method of the http.ResponseWriter. We can create a new struct that embeds an http.ResponseWriter and implement the WriteHeader method to store the status code in our new struct. This allows us to log the HTTP status code from the response, which is a crucial part of the response.
You might enjoy: Golang Copy Struct
Context
Context is a package in the standard library that helps manage the lifecycle of values across a request or operation. It's like a container that holds and passes values around.
The author of the blog has been busy closing on a new house and moving, but they're back to writing about context. They're starting a short series on the context package, explaining what it does and offering advice.
Related reading: Create a Package in Golang
Adding Logging
Adding logging to your application can be a game-changer. It helps you understand what's happening behind the scenes and can even save you from debugging headaches later on.
To log all requests, you can create a function that takes a http.Handler and wraps it, returning a new handler that logs some information about the request before invoking the wrapped handler. This works as expected, but you might want to log the response as well.
Logging the response is a bit trickier, but you can do it by creating a new struct that embeds an http.ResponseWriter and implements the WriteHeader method to store the status code. This way, you can access the status code and log it later.
By using this approach, you can log the HTTP status code from the response, which is a crucial part of the response. This can be especially useful when debugging your application.
On a similar theme: Golang Source Code
Data Conversion
Data Conversion is a crucial aspect of working with Go, and it's essential to understand how to convert between different types.
Go has a built-in function called `strconv.Atoi()` that converts a string to an integer.
This function is useful for parsing user input or reading data from a file.
The `strconv.Atoi()` function returns an error if the conversion fails.
You can use a simple `if` statement to check for errors and handle them accordingly.
For example, `strconv.Atoi("123")` will return `123` as an integer.
But `strconv.Atoi("abc")` will return an error because "abc" cannot be converted to an integer.
Go also has a `strings` package that provides functions for working with strings.
One of the most useful functions in the `strings` package is `strings.TrimSpace()`, which removes leading and trailing whitespace from a string.
This function is handy for cleaning up user input or data read from a file.
For example, `strings.TrimSpace(" hello world ")` will return the string "hello world".
Go's type system is designed to prevent common errors like type mismatches.
However, sometimes you need to convert between types explicitly, which is where functions like `strconv.Atoi()` come in handy.
In the next section, we'll explore more advanced topics in Go, including error handling and concurrency.
See what others are reading: Hello World in Golang
Frequently Asked Questions
What is W in Golang?
In Go documentation, "W" is a placeholder name for an argument, used to avoid lengthy descriptions. It's a common convention in Go that helps developers quickly understand code.
Is Netflix using Golang?
Yes, Netflix is using Go (Golang) for building internal tools, including Chaos Monkey. This highlights Go's suitability for high-performance systems.
Is Golang still popular in 2025?
As of 2025, Go (Golang) remains a popular choice among developers, with 13.5% of worldwide developers and 14.4% of professional developers preferring it. Its strong position in the developer community suggests it will continue to be a relevant language in the industry.
Featured Images: pexels.com


