Golang for Java Developers

A quick look at a few important differences between Java and Go

Lars Gröber
Apr 12 · 5 min read

At Inheaden, we love learning more about new technologies and tools. Oftentimes, a new tool replaces the old one if it brings enough benefits. That’s what happened with Go and Java. After testing out Go as a backend language, we decided to completely switch to Go for any (new) microservice we would write. This article will shine some light on the differences you need to know when switching from Java to Go.

This article will only look at a few important differences between Java and Go and does not strive to be complete. You can try out Go yourself by running any piece of code in this article on the Go Playground.

Also, make sure to stay tuned for more articles in the future about how and why we switched technologies on the backend and what we learned on the way.

Variables

Of course, both Go and Java support the usage of variables, but Go has a few specifics here. Mostly syntax related, though.

As you can see, new variables are defined using := while changing their value requires just =. Using var on the other hand requires only a single =.

Pointers

A large difference between Java and Go is the usage of Pointers directly in Go.

The syntax is very similar to how C does it, although Go does not support pointer arithmetic. Luckily, Go has garbage collection, so we do not need to worry too much about memory leaks.

Pointers allow you to pass any variable by reference instead of by value (or at least make it work that way — Go does not have the concept of a “reference”). Most types in Go are passed by value if they are not pointers. Slices, maps and interfaces are always handled like pointers.

Slices and maps

Slices are the Lists of Go while maps are similar to HashMaps that essentially are key-value stores. Go also has fixed-sized arrays, but we don’t cover those here.

As you can see, slices are variable-sized lists that also support Python-like indexing (or “slicing”). You can convert a fixed-sized array to a slice by calling fixed[:]on the array.

Maps, on the other hand, are implemented as Hashmaps and allow you to quickly lookup a value by a key.

Error handling

Probably the biggest discussion point with Go is its error handling — or lack thereof. Go has no concept of “throwing an error” or the typical try/catch blocks you find in Java and many other languages.

In Go, errors are returned “normally” from a function and you as the caller are responsible for handling them.

In this example, you see the typical way someone would handle errors on a program. You will see the if err != nil part in a lot of Go programs. While this becomes seemingly annoying at some point — and sometimes, it does — it forces you to always think about when errors could happen and how to handle them. In Java, you often simply rely on some other part of the code to handle the errors that might get thrown.

Go has a way to “throw” errors, though: panicking. But that should usually be reserved for nil dereferencing or type assertion errors.

Interfaces and structs

Lastly, let’s look at interfaces and structs in Go. For both, Go and Java, interfaces are used to describe the public API that some implementation should offer and they are especially useful when extending a library or writing tests. While you have to explicitly implement an interface in Java, in Go, interfaces are implemented implicitly — you just define all methods on a Go struct that the interface demands.

Structs, on the other hand, are the classes of Go. They can have members and methods like a class in Java.

Note here that dog is of type Animal not *Dog and that interfaces in Go have to be pointers. You sometimes see a line like var _ Animal = &Dog{} which asserts that Dog implements Animal.

Conclusion

Switching technologies is always a daunting task, but we hope that we were able to help anyone that might be thinking about changing from Java to Go or who is simply interested in how writing Go differs from whatever language you have used so far.

Thank You for Reading!

Found this post useful? Kindly hit the 👏 button below to show how much you liked this post!

Inheaden is a young IT and software startup based in Darmstadt, Germany. As an “Idea and Tech Factory”, we have set out to be a driving force of innovation, digitization, and automation with a focus on the areas of services, products, and research. Under the Inheaden brand, we work on individual “high performance” software solutions that bring a change. Modern designs, innovative technology approaches, and IT security for our partners and customers are important components of our work profile.

Inheaden

Progress through Innovation

Inheaden

Follow Inheaden’s journey on becoming an established software company. Follow our deep tech insights, state of the art technologies, design inspiration, business trends & real team stories . All Straight out of our Inheaden’s community & team of IT, design & business enthusiasts.

Lars Gröber

Written by

CTO & Co-founder @inheaden

Inheaden

Follow Inheaden’s journey on becoming an established software company. Follow our deep tech insights, state of the art technologies, design inspiration, business trends & real team stories . All Straight out of our Inheaden’s community & team of IT, design & business enthusiasts.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store