Posted Oct 17, 2023 at 11:47 AM
Edited Dec 17, 2024 at 8:32 PM
I just finished porting this site over to Go and I wanted to make a blog post about the experience. I’m not going to go into much detail about the code, since anything that isn’t straight out of Alex Edwards’ Let’s Go! is extremely similar to the python implementation. Instead I wanted to talk about the experience more generally. Things I liked/disliked, anything that stuck out as weird, etc.
I’m going to get the things I didn’t like out of the way first. I don’t like the implicit interface implementation. In Go, as long as a struct has the functions described in the interface it is implicitly considered to have implemented that interface. I would prefer it if there were some statement saying that this function is implementing this interface, kind of like what Rust has for traits. As it stands in Go it can be difficult to tell what interfaces an object implements from just looking at the code.
Speaking of things that can be hard to tell from just looking at the code, Go provides a foot-gun by letting you define a struct in one file and it’s methods anywhere else in the same package. This can make it extremely difficult to figure out what you can do with a struct. Luckily this one isn’t too bad if you have an LSP and some kind of code-completion you can tab through, and can be avoided altogether by just having some self-control.
The first thing I like about Go is the standard library. It’s honestly fantastic for web development. I came into the language expecting it to be like Python or JavaScript where you really do need a framework to get going, and was pleasantly surprised to find that in Go the standard library is pretty robust. It’s to the point where many of the libraries that do exist are essentially just wrappers around the standard library; practically syntactic sugar. I was very impressed by this. There were only a couple things I felt the need to step outside of the standard library for: routing and composing middleware chains. Admittedly, I could have gone without Alice for composing middleware chains, it just would have been a little clunkier, but the router definitely needs some work. You should be able to call different handlers for different HTTP methods and have URL parameters. I don’t know how that’s been overlooked for so long, and I’m glad it’s getting fixed in Go 1.22. Other than that, the standard library is fantastic.
Another thing about Go that I surprisingly found myself liking is the error handling. Apparently this is a controversial topic, but I don’t actually mind typing if err != nil
all the time. I like that when you get an error it doesn’t break control flow, as it does in languages that have try/catch. I know exactly how execution will proceed at all times. I like how anything that can error tells you it can error by returning an error value that you have to deal with. There’s no mystery about “does this function throw an exception?” It tells you.
Next, I love that it’s strongly and statically typed but still has type inferences. After working with Python (without type annotations) and JavaScript for so long, I’ve missed strong static typing. I love that I know exactly what type every variable is, that it won’t suddenly change types, and that I don’t have to do any type checking on things that get passed to my functions. At my work whenever I come across a new function in our codebase it can sometimes be a pain to figure out what type it expects to take and I’m sick of that. Go felt like a breath of fresh air.
Finally, I love that it’s a compiled language. All kinds of errors that would only be caught at runtime in Python and JavaScript are caught before I can even try to run the application in Go. There’s entire classes of errors that I don’t have to manually check for because the compiler does it for me. Additionally, I like that I can just deploy a binary. I don’t have to upload the entire project source to the server and install dependencies just to run it. I can just build the binary, copy it over, and run. It’s so much easier.
So that was what I liked and didn’t like about rewriting in Go. Now that I finished the port, expect new features to be added and blog posts about those features. Hope you enjoyed the read!
Bonus Bail:
Bail suffered a leg injury at the dog park a couple weeks ago and has been on bed rest. So we got him a little friend to keep him company. I think he likes him.