Writing a Web Server in Go/Postgres/Docker

Go is fun but I worry it is still a toy language

Go is fun but I worry it is still a toy language

I am documenting my experience today writing a web server in Go.

Today we chose to study Go for its simplicity, performance, and concurrency features, while PostgreSQL was selected for its reliability and robustness as a relational database.

Leveraging Docker for containerization provided a seamless environment setup, enabling us to encapsulate our Go server and PostgreSQL database within isolated environments.

I have been doing memory-safe Rust for a bit and was curious about learning some Go considering they are starting to be used to tackle cloud-based computing issues, especially with web servers.

Crafting the Go Server:

With the environment ready, we delved into crafting our Go server.

We structured our server code using best practices, embracing modularity and scalability.

Utilizing Gorilla Mux for routing and HTTP handler functions, we efficiently managed incoming requests and defined API endpoints.

Implementing middleware for logging, error handling, and authentication enhanced the reliability and security of our server.

The statically typed nature of Go empowered us to write clean, concise code, reducing the likelihood of runtime errors.

Compared to Rust

As someone who has previously worked in web development, using Go is easier than Rust.

I believe that the developers attempting to shoehorn Rust into web development have good intentions, but it should certainly be left for low-level in place of C or C++. At least for my projects, other developers may not have these same personal pitfalls.

Let me explain.

Rust’s strengths come from its borrow checker and unique way of handling memory.

When writing memory-critical applications, such as databases, adhering to Rust’s guidelines can produce elegant code that is bug-free.

Go does not offer the same sort of memory handling but in the web space, that is not as important for the majority of applications. Time to market can be critical and Go allows for less experienced developers to get products out quicker.

func main() {
_, err = db.Exec(sqlCommands)
 
if err != nil {  
log.Fatal("Error executing SQL commands: ", err) } 

fmt.Println(sqlCommands)

}

This approach allows for concise and readable error handling without needing to explicitly check for error conditions after every function call, which can be cumbersome.

In Rust, the borrow checker enforces stricter rules around error handling, requiring explicit handling of error values at every step, which can lead to incredibly verbose code.

Rust can always replace Go code but not necessarily the other way around, so Rust developers should be more open to writing web-based projects in a language like Go.

Conclusion

In conclusion, I will continue to use Rust for low-level projects but it is unlikely that I will write a web server anytime soon in Rust.

Go allows me to work faster & not fight the borrow checker for, as far as I'm concerned, problems that have already been solved in the web world.

-Nick

Reply

or to participate.