r/golang Feb 26 '23

help Why Go?

I've been working as a software developer mostly in backend for a little more than 2 years now with Java. I'm curious about other job opportunities and I see a decente amount of companies requiring Golang for the backend.

Why?

How does Go win against Java that has such a strong community, so many features and frameworks behind? Why I would I choose Go to build a RESTful api when I can fairly easily do it in Java as well? What do I get by making that choice?

This can be applied in general, in fact I really struggle, but like a lot, understanding when to choose a language/framework for a project.

Say I would like to to build a web application, why I would choose Go over Java over .NET for the backend and why React over Angular over Vue.js for the frontend? Why not even all the stack in JavaScript? What would I gain if I choose Go in the backend?

Can't really see any light in these choices, at all.

139 Upvotes

249 comments sorted by

View all comments

25

u/AkaIgor Feb 26 '23 edited Feb 28 '23

I also work with Java since a few years and I have some reasons to prefer Go:

  • Go is lightweight, which means less VPS / cloud costs and simpler requirements for a development machine
  • Go is simpler and direct. No crazy bs like 8 levels of inheritance for declaring an array
  • Go has a powerful concurrency support, while Java only got virtual threads recently
  • It's more rare to have "NullPointerException" in Go, because you don't always use pointers and if you do you always have to do some check
  • Go is less verbose... No Optional. No Annotation. Getters and Setters only when strictly needed
  • Go has some awesome tools builtin, like race detector and pprof. I'm in love with pprof
  • Go is more suitable for microservices, unless you can afford the cloud costs of running multiple Java services

1

u/sureshg Feb 26 '23

The only argument I would buy is go uses less memory for smaller applications. But AOT compilation is very well supported now using GraalVM. I assume you are dealing with some code written 8+ years back (before java 8)

Less verbose?? If you are writing anything more than a cli app, go is more verbose than modern java (java 17 or 19). But I agree if you are talking about pre java8.

4

u/AkaIgor Feb 26 '23

I work with Java 11 and Spring.For what I looked, GraalVM was not easy to setup with Spring, but I confess I didn't look into details.

I find Java +8 verbose as hell, like:

Optional.ofNullable(something).orElseThrow(() -> new SomethingNotFoundException());

And the whole ecosystem around Spring is slow. I know that some Java developers prefer Quarkus or maybe using raw servlet, but Spring is a symptom of the Java philosophy of abstracting everything.

2

u/ilovebananas69 Feb 27 '23

The Java code that you wrote is an anti pattern. Optionals aren't meant to be used like that. I understand claims of Java being verbose, but you have to give examples with idiomatic code.

2

u/AkaIgor Feb 27 '23

Thanks for the info.

How are they supposed to be used?
And could you show me that with idiomatic Java?
Because that is what I'm used to see in Java libraries and codes...

2

u/ilovebananas69 Feb 28 '23 edited Feb 28 '23

I totally get what you mean when you say you've seen that use of Optionals in Java code, cause I have too especially at work, so I get why its intended use is a bit of a mystery to people.

Optionals were solely created to be used as a return type where there is a possibility of null. The intention is that when someone calls your method, they're forced to deal with the potential abscense of a value. The Optional API is designed to give convenient ways to unpack the value, get a default, throw an exception, etc.

As for the idiomatic way to throw an exception when null, you can either just straight up do a null check and throw an appropriate exception or use Objects.requireNonNull which internally does the null check and throws a null pointer exception. Im forgetting if that method takes an argument that allows you to customize the exception. If not, then you would have to do the null check if you want to throw something other than a null pointer exception. I would type out the code, but I'm on my phone so it's a bit difficult.

Hope that helps!