Three, two, one, Go!

How Go’s typing system differs from OOP in Java

Sohel Ather
© Shutterstock / A. and I. Kruk

Go might be a free time programming language, but it’s gaining fans. In this article, Sohel Ather explains why Go is on the upswing and how it differs from Java.

In the past few years, Go has gained huge popularity. It is one of the best programming languages of choices for some cool new technology such as OpenShift, Docker, and Kubernetes. Golang, or Google Go as it is often referred, was especially designed by some of the well-known system researchers, including Rob Pike of Plan 9 fame and one of the creators of C,  Ken Thompson.

Pardon my pun, but this language has a lot “go” ing for it. Google has started using it for internal projects. In web development and scripting communities, there is a huge interest in Go language. The language also has a mature implementation that is a lot easier to supervise when considering new programming languages.

When it comes to talking about the type system of the language, it is a highly important feature of a programming language that enables you to organize your application data. In addition to this, the language is known for following at least approach for its type system. It also delivers varied built-in types like string, int and float64 and, bool.

Go supports compound types like array, slice, map, channel and more. Go’s complex types are made up of other types – built-in types and user-defined types. Let’s take an example – the composite type “map(string)string” shows a complete range of string values. In every single string value perseveres with a string key as here values can be easily saved with the corresponding key. Let’s have a look how Go’s typing system differs from object-oriented programming in Java.

Local variables

In Java, local variables are mainly stored on the stack.  It is also real in C, C++ and in other languages, where call-stack as such is applied. This language is no exemption. Apart from this, you can easily return a pointer to a local variable from a function.

In C, it was a deadly mistake. However, in Go, the compiler knows that the allocated “object” is absconding the method and allocates it as per it. Therefore, this language continues the return of the function and the pointer that will not point to an already abandoned memory location, where there is no reliable data.

Thus, it is legal to write:

package main

import (

type Record struct {
	i int

func returnLocalVariableAddress() *Record {
	return ℜcord{1}

func main() {
	r := returnLocalVariableAddress()
	fmt.Printf("%d", r.i)

Code Source

Types defined with structs

In Go, structs are the way to develop user-defined concrete types. However, the design of the struct type is completely unique as compared to other programming languages. In other words, a struct is a set of different fields or properties.

However, Java language provides a class keyword, whereas, Go does not offer a “class” keyword. Instead of a class keyword, Go offers structs that is a lightweight version of classes. A struct is one type of exported into other packages if the name of the struct begins with a capital letter.

In the Go programming language, there is no need to use getters and setters on struct fields. They can be easily retrieved from the same package and can access from other packages if the name of the fields begins with a capital letter.


You are able to write functions inside functions, and you can return functions just like in a functional language, and the local variables across it serve as variables in a closure.

package main

import (

func CounterFactory(j int) func() int {
	i := j
	return func() int {
		return i

func main() {
	r := CounterFactory(13)
	fmt.Printf("%d\n", r())
	fmt.Printf("%d\n", r())
	fmt.Printf("%d\n", r())


Object orientation

Go is just as object oriented as JavaScript with its closures and functions being first class citizens. In reality, this language is more than that as it comes with interfaces and structs. It is true that they are not completely classes. They are value types.

These are passing by only values. Wherever they are stored in memory, the data there is only the pure data and no object header is like that. While structs in Go are much like they are in C, i.e. containing different fields; they can’t extend each other and can’t contain methods.

SEE MORE: Go is a ‘free time’ programming language, survey shows

Object orientation is advanced a bit different. Rather than including the methods into the class definition, you can stipulate when you define the method itself which struct it applies to. Structs also contain some of the other structs.

In case, if there is no name for the field, you can reference it by the type of it that becomes its name implicitly. Apart from this, you can just refer a field or any technique that belonged to the top struct.

Here’s one example:

package main

import (

type A struct {
	a int

func (a *A) Printa() {
	fmt.Printf("%d\n", a.a)

type B struct {
	n string

func main() {
	b := B{}
	b.A.a = 5
	fmt.Printf("%d\n", b.a)

It is almost a kind of legacy. When you agree the struct on which the method can be appealed, you can specify the struct itself or pointer to it. If this method is executing to the struct, the method will access a copy of the caller struct.

However, if the method is applied to a pointer to the struct, the pointer will be passed. In the final case, the method or technique can be customized the struct. Either the method can be used to fulfill the needs of an interface.

Go is very compressed

When it comes to Go, it is extremely compact and not forgiving. There are some of the characters that are simply useless in other languages. In the last 40 years since C was invented, we got used to them and all other languages that followed the syntax.

However, just because it’s old doesn’t mean that it is good. We all understand that the trailing else problem is best addressed using the {and} around the code branches in the ‘if’ statement. If we have the braces, there is no point to enclose the condition between parentheses. Have a look at the code below:

	if a == nil {
		fmt.Println("a is nil")
	} else {
		fmt.Printf("%d\n", a.a)

There is no requirement. Go does not even allow it. You can notice that there are no semicolons. You can use them, but you don’t need to. On the source code, inserting them is a preprocessing step, and it is effective.

Most times, they’re clutter anyway. In order to declare a new variable and assign a value of it, you can use :=. On the right-hand side of the expression, it usually shows the type, so there is no need to write var x typeOfX = expression.

Apart from this, if you import a package and assign a variable, but don’t use it afterwards, it is a bug. At the compile time, it can be easily detected as a code error, and the compilation fails.

Some other things

Go has its own little quirks. These things may also sound weird at first, but developers can get the hang of them.

  • Source code of libraries are to be imported into the source of the project.
  • Public functions and variables are capitalized as their various keywords such as public and private.
  • Code generation support built into the language in forms of comment directions.
  • Lack of generics.

In short, Go is an exciting and interesting language. However, it is not a replacement for Java, even on a language level. Both these languages are not serving the same type of tasks – Java is enterprise development language, whereas Go is a system programming language. In case, if you have any questions, you can get in touch with an experienced developer who is familiar with Go as well as Java language.



Sohel Ather

Sohel is consultant and blogger at SpaceOtechnologies company, a leading mobile app development company. Sohel focuses on actionable and unique ideas that can be used to grow company business and make a real difference for customers. You can follow him on LinkedIn.

Leave a Reply

Be the First to Comment!

Notify of