Intro
I recently learned Go with the help of a book called "Let's Go" 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:
- If you are writing JS you just don't even know if a function that you are running is going to err
- 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.
- 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 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, 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
Email: aaravsibbal.15@gmail.com
Top comments (4)
For me as a sysadmin and infrastructure engineer who codes utilities, I like Go as it's rigid and won't allow you to write messy code. It's super fast and the ability to code utils once and compile for Win/Lin on either platform makes it super flexible. I don't like Python and Java as they require you to have large runtimes installed. I did code in Java for many years and again it was the static typing that I loved but the 250Mb runtime was such a pain. I tried learning Rust but I just couldn't get on with it, I found Go to be easy to get started with and really fun to code.
Yes, I felt the same way! It is nice to know people more experienced than me like the same things about Go as me.
I like how the language guides you as you write code. My first ever go project has run un-attended for over 6 months without crashing or needing any kind of updates. There is no way I could've pulled that off on my own with any other language.
That's amazing I am actually in the process of writing a production project right now and I just can't believe how fast I can go with this. Your story gives me comfort lol.