Go is based on C program syntax achieves max effects with min means but Java JRE is written in C
Go started as system programming language but widely accepted now as general-purpose language, Java renamed from Oak
Both are Typed language
Go proven to have better performance than Java
Go is a small, simple, easy to learn, easy to use and really fabulous language
Go provides standard libraries comes with “Batteries included” :) and decentered libraries but Java usages centralized approach like Maven
Go is kind of reduced set or stripped features of Java
No VM in Go but Java is based JVM
Go usage struct for complex data type and great support for JSON data handling and it is easy to implement with struct
Go implements some of the object-oriented features in very unusual approach
Go supports Pointers and Address unlike Java
No inheritance in Go, complex object behaviors are created by composition
No Method Overloading in Go
Two functions/methods cannot have same name in a package in Go
The panic and recover feature in Go are similar to the Exception handling in Java
Go concurrency model is based on Go routine something different than thread
Go also support automatic memory management
Go playground on web for playing with code and sharing
Side by side comparison
1. Java and Go both are strongly Typed
Java: Strongly typed, with explicit type declarations enforced at compile time.
Go: Also strongly typed, but offers type inference, allowing for more concise variable declarations and makes little easy by short variable ( := ) declaration
2. Package Management:
Java: Relies on tools like Maven etc. for dependency management, with centralized repo for libraries and packages.
Go: Utilizes a decentralized approach to package management with the go get command and the go.mod file, allowing us to specify and manage dependencies within their projects.
Code Organization in Go
Workspace
Module - Something like Maven for Java
Package
Go package has no dependency on folder(s) name of go source, but one folder must have only one package.
Go package always short name, without dot (.) unlike java and has no relation with folder name.
Go package while importing the package, need module name and folder(s) where package exist.
Go program Execution always starts with main package and function main.
Java - Access control by declaring variables, methods and class as public, private, protected and default.
Go - Access control by Private and Public is by the first letter of the name for variable or function both. If the letter is lowercase, it is private. If it is uppercase, it is public. The scope of a private function/method/type is the package in which it is defined.
4. Unused variable and Blank identifier variable
Java - No complain and alright to define unused variable or not receiving return values of method.
Go - Unused variable, unused import gives compile time error but that can be replaced with underscore ( _ ) known as blank identifier.
5. Initializing variable or perform initialization during code load time
Java- When class is loaded in memory, static variables or code block is initialized.
Go- Supports init() method that is called when program is imported. These init() functions can be used within a package block and regardless of how many times that package is imported, the init() function will only be called once. if just want to call init() method then import package with Blank Identifier
6. Global variable
Java: Java got access modifier like public, private, protected and default attached to and even volatile to update the value from different threads update
Go: Global variables in Go are editable. Any code in any of the functions can edit the global variable.
7. Pointer and Address
Java references: Automatically managed memory; pointing to an object abstracts away the memory address.
Go pointers: Explicitly point to memory addresses; changes made through a pointer affect the original variable.
Java: Purely object-oriented, where everything is an object and classes are fundamental.
Go: Supports some OOP principles like structs and methods, but also encourages composition over inheritance., Go implements it for structs only in unique way
Java: there are many loggings' libraries support including jdk logger and log4j
Go: provide basic logging, and recent version supports slog (structured logging). Rolling file and some of features need to use third party library support.
12. Unit Test
Java: JUnit is an open-source Unit testing framework for java
Go: Unit testing is an inbuilt tool/command for conducting automated tests
Java: Supports annotations for adding metadata to classes, methods, and other program elements, used for various purposes like configuration and code generation etc.
Go: Annotation is a go package that allows parsing and read Java Style annotation strings e.x @SomeAnnotation(param="value"), this package is used by go-service to read and parse annotations in comments.
16. Concurrency Model
Java: Uses threads for concurrency, with libraries like java.util.concurrent for synchronization and coordination.
Go: Offers built-in support for concurrency with go-routines and channels, making it easier to write concurrent programs compared to Java's thread-based model.
Go routine is something like Virtual Threads (part of JDK 21)
17. Stacktrace and dump
Java: thread dump and stack dump.
Go: supports Runtime API for Stacktrace dump
Similar to Java, SIGQUIT (kill -3 <GO_PROCESS_PID_ID> or if process running prompt is available then press ^\ keys ( CTRL+\ )) can be used to print a stack trace of a Go program and its goroutines. A key difference, however, is that by default sending SIGQUIT to Java programs do not terminate them, while Go programs do exit. This approach requires no code change to print a stack trace of all goroutines of existing programs. e.g.
Java: Garbage collection is handled by the JVM's garbage collector, which is optimized for Java's memory management.
Go: Has its own garbage collector optimized for concurrent execution, with control over memory allocation and deallocation. Go implements concurrent, Tri-Color block (White-Balck-Gray), Mark and Sweep policy and uniquely implemented stack and heap memory management.
Garbage collection in Go
Manages memory allocation and deallocation automatically.
Picks up anything out of scope or nil
GC is triggered based on trigger ratio
GC runs concurrently using mark and sweep, Tri-Color block (White-Balck-Gray)
The GOGC is environment variable sets the initial garbage collection target percentage. A garbage collection is triggered when the ratio of freshly allocated data to live data remaining after the previous collection reaches this percentage. The default is GOGC=1OO.
Setting GOGC= off disables the garbage collector entirely.
Java: Code is typically executed on the Java Virtual Machine (JVM), providing platform independence but using GraalVM, Java code can be converted into native binary
Go: Code is compiled directly to machine code, resulting in faster execution and simpler deployment, but sacrificing some platform independence so it's Fast native code
Engineering Leader | Strategic Accounts | Growth & Innovation focused
1yGreat read