top of page
  • khangaonkar

Go review: Should I use the Go programming language ?

Overview

Go was developed by engineers at Google. Their primary motivation was dislike for C++. Goal was to create a language for systems programming. Its popularity has been slowly but steadily increasing.


The purpose of this blog is to help you decide whether Go is the right language for you. Why Go and not C++ or Java or Python? Of course, as you read you will see my personal bias. When I say Go documentation is not good, that is based on my experience and comparing with documentation of other languages I have used. If you disagree, feel free to comment on why you think otherwise.


In general, how is the programming language chosen ? Sometimes the choice is obvious due to the public knowledge base such as Javascript for UI development or python for AI, C/C++ for low level systems programming. But if you are developing vanilla backend micro-services, more than one language can be suitable. Unfortunately personal bias plays an important role in the choice in such cases. In such cases, more often than not, the chosen language is the personal favorite of the VP, CTO, architect or early programmers.


More on the bias -- my experience on Twitter and LinkedIn and even at work is that discussions on programming languages are more religion than science. Post a message that you dislike a language because of any reason and you are likely to get trolled by fans of that language.


And why is choosing the right programming language important ? The cost of a using the wrong programming language is significant due loss in programmer productivity, increased time to market for product and potentially lower performance.


In the rest of the blog I am compare Go to the popular backend languages C/C++, JAVA, Python, Javascript (Node).


Programming Style

JAVA is pure object oriented language. Other than trivial programs, there is almost nothing you can do without classes, interfaces and Objects. C is procedural. C++ is object oriented language but you can still write procedural code.


Go on the other hand is a more procedural language like C. You write your code as functions and data is passed around as parameters. Compound data types called struct are often use as parameters. Nothing wrong with being procedural. But the language also tries to support some object oriented concepts. Go has the concept of receivers by which a function becomes a method that can be called on a type. Interfaces are supported. But you never have to explicitly say "Type A implements interface B". If type A has received methods that match interface B, then it is said to implicitly implement the interface. Inheritance is based on composition only. In all, the Go OO like features are non intuitive.


Simplicity and learning curve

When I learnt JAVA, Javascript, Python, C, C++ -- each of these languages made sense. When a language is well designed, it is intuitive and one can write code without frequently have to refer to the manual for syntax.


Learning Go is probably easier that C/C++ but harder that JAVA, Python, Javascript.

In Go, some concept like receivers, implicit implementation (interfaces), channels can be non intuitive and take some time getting used to.


Compilation

Javascript and Python are considered interpreted languages.


In JAVA, the code is compiled to bytecode. The JVM starts by interpreting bytecode and then does just in time compilation to native code as needed. JAVA is considered a compiled language.


Go is a compiled language as are C/C++.


Languages compiled to native code execute faster. C/C++, Go and JAVA have the advantage over Javascript and Python.


Standard Library

The standard library of the language provides useful classes or functions for common data structures, algorithms and domains like concurrency, networking, security etc.


The other languages have a richer and more robust standard library than Go. For example java.util.collections , java.util.concurrency or even c++ std library have a rich collection of utility classes that can be easily used.


Go Concurrency

Traditionally the thread libraries of C/C++ and the threading model of JAVA have been based of operating system threads.


Go has been one of the early adopters of the virtual threads paradigm which provides more concurrency. This contributed to the claim that Go is more scalable. However recently JAVA has added support for virtual threads. So any concurrency advantage with respect to JAVA is no longer valid.


Goroutines communicate using channels. The producer of data writes to a channel and the consumer of data reads from the channel. Inter-thread communication is simpler.


Threads in the OS based model generally communicate by sharing references to some location in memory.


Memory management and Garbage collection

It is automatic for both Go and JAVA. Most of time developers do not need to be involved. The concepts of stack and heap are similar. Local variables, functions arguments and call stack is on the stack. Objects allocated dynamically are stored on the heap.


The Go garbage collector is a concurrent mark and sweep garbage collector with a goal to eliminate the stop the world GC pauses that have historically plagued JAVA. During the mark phase, the GC scans the heap and marks objects that have references to them. During the sweep phase, the GC frees up memory for objects that have no references to them. Live heap is memory marked live by the GC. New heap is heap not analyzed by the GC. The GOGC param default value 100 is used to control when GC is trigger. The value means the size of new heap is 100% of the live heap. Increasing the value means less frequent GC. Reducing the value means more frequent GC. The GOMEMLIMIT parameter set the max memory the GC runtime can use, after which it will throw an OOM exception. The seems similar to the JAVA -mx parameter.


The current default JAVA GC is a generational garbage collector. Recents objects are in the new regions "eden" and older objects are moved to other regions. The eden is garbage collected more frequently than the older region. JAVA supports other garbage collectors and provides a few bells and whistles for the developer to tune the GC. GC of the older regions involves pauses.


For C/C++ the programmer has to explicitly release dynamically allocated memory.


Go supports pointers.


Go Performance

The common reason for using Go is generally better performance and lower startup times.


Without much discussion we can easily say Go will outperform Javascript and python. We can also say the C/C++ will outperform Go.


A discussion of Go vs JAVA performance is worthwhile. Some claim that Go execution is faster because it compiles code to native code, whereas JAVA compilation produces bytecode that is executed by the JVM. But JAVA has had just in time compilation to native code for long time with execution performance comparable to languages that compile to native code.


The Go concurrency model is based on virtual threads which has significant performance advantages over Java concurrency model which is based on native threads. However this advantage is likely to go away. JAVA has introduced virtual thread in JDK19 and structured concurrency in JDK21. Even though these are preview features right now, they will become full features soon.


Lastly start up time for Go code is lower than Java, with Java having a little overhead of having to start the JVM. For long running programs this is not a big deal.


Some code and comparison:


Documentation

The official documentation for JAVA, Javascript and Python are far superior to the official Golang documentation. The Go SDK docs are one of the worst (personal opinion). You need to rely on stackoverflow and chatgpt. Even c++ std library has better documentation.


Ecosystem

JAVA, Javascript and Python have a huge rich ecosystem of third party libraries for almost anything. The same cannot be said about Go.


Summary

Of Course, Go can be a very useful programming language on its own merit. The creators set out to create a systems programming language. Has it achieved the intended goal ? My assessment is:

  • Concurrency: Simpler and more scalable that C/C++. But Java is catching up.

  • Memory management: Advantage C/C++. Go has garbage collection like JAVA.

  • Compilation: No advantage. C/C++ and JAVA all have compilation.

  • Programming model: Advantage JAVA. Even C/C++ have a cleaner model.

  • Ecosystem: Advantage JAVA, Python. Even C++ might be better.


So if you are C/C++ programmer who is unhappy with state of affairs, unhappy with specific features, Go is a language you should consider. But do look at Rust as well. However if you are developing code for a space that is addressed by JAVA, Javascript or Python, there is no reason to even consider Go. The ecosystems of those languages are far more evolved. Go can be useful from systems programming where garbage collection is not an issue. But for general purpose backend application programming there are better languages one of which is JAVA.



Recent Posts

See All

Go Tutorial: Receivers

When I was new to the Go programming language and saw this syntax func (m *Service) addNumbers(a int32, b int32) int32 I was confused. I had not seen syntax like (m *Service) between func and the func

JDK22: New features in Java 22

JDK 22 was released on March 19, 2024. As always, a new suite of goodies is made available for the benefit of the Java programmer. Many of them are in preview. This is a very brief listing of the new

Comments


bottom of page