Making the Jump to Go: A Guide for Java Developers

jump to go

If you’re a Java developer like me, chances are you’ve heard rumblings of a trendy new language that came out of Google: Go.
And if, like me, you’re always looking for ways to code faster and better, you may be asking yourself whether any of your existing applications are good candidates to move to Go. While not every Java application should be ported to Go, in many cases, Go is a more productive development framework than Java. There is, therefore, a great deal of value in understanding what Go can do; where it builds on the strengths offered by Java, and where it differs.
To that end, this article will explore:

  • How to get started with Go development
  • The key differences between Java and Go
  • Whether Go is a better choice for application development than Java today

Getting Started with Go

Go’s mascot, designed by Renée French
Go’s mascot, designed by Renée French

The first and most obvious difference between Java and Go is that Go is not compiled into bytecode and executed on a virtual machine. Rather, Go applications are compiled statically with all their libraries into machine code and can be executed directly on the host machine.
While you can always download the Go source code and compiler from the community and install it yourself, the freely available ActiveGo from ActiveState can get you up and running quicker with their simple installers for Windows, Mac and Linux. ActiveGo is derived from and 100% compatible with community Go, but ActiveState is the only vendor that can also provide you with commercial support when you need it.
Golang.org and other websites (including ActiveState) provide extensive documentation and guidance for new users, which includes walkthroughs to set up, compile and execute your first application.
An additional benefit of Go is that given the short history of the language, much of the documentation available is current and applicable to modern applications. Contrast this with Java, where a quick search may yield the answer to your question with an example written using Java 1.4.

Some Key Differences from Java

Having C as a common, albeit distant, ancestor, Java and Go are similar enough that you should be able to make your way through a Go program and understand what’s going on. What follows is a list of differences I noticed during my first sojourn into the Go universe.

“Package” might not mean what you think it means

The package definition for the application that you want to compile into an executable is always package main. If you want to create a library which can be used as part of another application, you will specify a different package name. For example, if you have a library that you want to include to provide custom validation functions, you would begin the files with package validation, and you would want to include all of those files in a folder named validation. Other applications could include this as import “validation”.

Case is your access modifier

In Go, if you want to expose your functions and variables to other parts of the application, their names must be capitalized. If your functions and variable names begin with lowercase characters, they will remain hidden from other parts of the application, or in Java terms, kept private.

Types can be inferred or specified after the name

The variable type can be explicitly defined following the name of the variable, or inferred by assigning it a value of a specific type. The following declares a variable counter of the integer type.


        var counter int

Whereas the following assigns a value to the counter from another function, if a function called calculateTax were to return a float value, then total would be initialized as a float type. Something else to note is the assignment operator used. Go uses the equal sign ( = ) as a standard assignment operator, but it also has the short variable declaration operator, which is a colon and an equal sign ( := ). This operator allows for the redeclaration of variables under certain conditions.

total := calculateTax(total, rate)

Speaking of functions, function return types are also specified after the name of the function. The function signature for the aforementioned calculateTax function might look similar to the code below. Note the return type included after the parameter list.


func calculateTax(total float, rate float) float {
        //Tax calculations performed here.
}

Returning multiple values from the same function

If you’ve ever faced the situation where having to return multiple values from a function was called for, but you didn’t want to go through the hassle of creating a new data object and all the overhead that goes with that, you’ll appreciate this. Go allows the developer to return multiple values from a function.
You might also notice that both the stateAmount and federalAmount variables are initialized by type inference.


func calculateTaxes(total float, stateRate float, federalRate float) (float, float) {
        stateAmount := total * stateRate
        federalAmount := total * federalRate
        return stateRate, federalRate
}

Bid overloaded functions adieu

In Java, you can have multiple versions of the same function with slightly different signatures, allowing you to handle various parameter types differently. Unfortunately, this is not the case with Go. The reason for this was to limit the necessity for type matching and to simplify applications. There are good arguments both for and against using overloaded functions, but I was sad to find they were not supported in Go.

Is Go a Better Choice for Your Application?

The answer to whether you should be using Go is very dependent on your environment and the projects you’re engaged in. So, it depends.
Go’s primary niche right now is server-side web applications, so if that’s what you’re focused on developing, then you should consider Go. But don’t throw Java to the curb just yet. Consider the talent base within your organization and the specifics of what you are developing to see if Go is indeed the better choice.
For mobile device development and other environments dominated by Java, however, Java should still be your primary language, at least for now.


Get started with ActiveGo for Linux, Mac and Windows, free to use in development or internal testing.
Download ActiveGo
Image source: https://pixabay.com/en/skiing-jump-sky-1569135/

Recent Posts

Tech Debt Best Practices: Minimizing Opportunity Cost & Security Risk

Tech debt is an unavoidable consequence of modern application development, leading to security and performance concerns as older open-source codebases become more vulnerable and outdated. Unfortunately, the opportunity cost of an upgrade often means organizations are left to manage growing risk the best they can. But it doesn’t have to be this way.

Read More
Scroll to Top