Things I like about Go
Tags: webdev, javascript, go, programming
Published: 2024-02-10 01:03:22

## Intro

I recently learned Go with the help of a book called "[Let's Go](https://lets-go.alexedwards.net/)" by Alex Edward. The book was nice, especially for beginners in Go/Web Dev like me, and there were a lot of things that I liked about the language. Before using Go the only real experience I had was with the JS eco-system so because of that most of my comparisons will be between Go and JS. 

---

## Error Handling

### JS Error handling

Most of the time in JS/TS error handling is done like so:
```
try{
	await sql.open("db")
}catch(err){
	// do something else
}
```

I feel that there are a lot of problems with this approach:

1. If you are writing JS you just don't even know if a function that you are running is going to err
2. If you are writing TS unless the people who have written the library have chosen to write the return types of the functions, you don't know that the function is going to err, and that just seems to be a recipe for a disaster. 
3. It is not even necessary to have errors as values

### Go Error handling

Error Handling in Go is done like so:

```
db, err := sql.Open("db")
if err != nil {
	log.Fatal(err)
}
```

Things I like about this approach:

Along with the fact that errors are treated as values and that you are explicitly required to write your return types allows for an experience where as a developer you know every function that is going to err in your codebase from which you can handle the errors accordingly. 

---
## defer

I love this functionality in every language that I see it in, because of its convenience. It allows me to not think about how and when the connection/db should be closed, it is just a nice quality-of-life feature.

```
db, err := sql.Open()
if err != nil {
	log.falal("internal server error")
}

defer db.Close()
```

---
## Standard Library

In JS the libraries you choose completely change the way that you experience/learn the language to the point that there is a difference between a JS developer, a TS developer, and a React Developer not to mention all the n number of libraries that are needed that make your programs that can also change your experience drastically.

Comparing this to Go where most of the things that you might need are already included in the standard library, moreover, the things that are included are useable this allows for more or less a singular experience with the language and I feel like this would work better with teams. A great example of this would be having the "net/http" package ready to go, which as of [release 1.22](https://tip.golang.org/doc/go1.22#library) also has improved the routing allowing for request methods as well as wild card at the end of the URL example: "POST tweet/{id}". 

Having a good standard library also makes it so that you are focused on making the project rather than focusing on the third-party libraries that you must choose in JS.

---

## Conclusion

I liked my experience with Go it was easy to learn, fast to be productive in, fast as a language, compiled, statically typed, with good error handling, a complete feeling standard library, and doesn't even have [colored functions](https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/), which just feels like a win to me overall. I'll be making a lot more of my projects using Go from now on and I am excited about it. 

Author: Aarav Sibbal
[Github](https://github.com/AaravSibbal)
Email: aaravsibbal.15@gmail.com