Learning Golang with a Java Background: A Developer’s Journey — Part II

Saravanakumar Arunachalam
4 min readJan 24, 2025

--

GOLAVA

Welcome back to the second installment of our journey exploring the transition from Java to Golang! If you haven’t yet read Part I, I highly recommend starting there for a smoother progression. In this article, we’ll delve into one of the core aspects of programming — collections. Specifically, we’ll focus on arrays, slices, maps, and their Golang equivalents. By the end, you’ll have a solid understanding of how these data structures function in Golang and how they differ from or align with what you may already know in Java.

Collections in Golang

Collections are the backbone of most programs, allowing developers to efficiently store, retrieve, and manipulate data. For Java developers, collections like arrays, ArrayList, HashMap, and HashSet are second nature. In Golang, we have similar constructs, but they come with their own unique traits.

Arrays and Slices

In Java, arrays are fixed-length collections, and dynamic resizing is achieved through classes like ArrayList. In Golang, we also have arrays (fixed length) and slices, which can dynamically resize. Think of slices as Golang's answer to Java's ArrayList.

Here’s a quick comparison:

  • Array: A collection with a fixed length.
  • Slice: A dynamic-length abstraction over arrays, offering flexibility and functionality.

Java Example:

// Array
int[] example = new int[]{1, 2, 3, 4};

// ArrayList
List<Integer> arrayList = new ArrayList<>();

// Adding the element
arrayList.add(1); // [1]

// Removing the element at the index
arrayList.remove(1); // [1]

Golang Example:

// Array
example := [4]int{1, 2, 3, 4}

// Slice
var mySlice []int // Declares a slice of integers with a nil value

// Slice with length
mySlice := make([]int, 5) // Length and capacity are both 5

// Slice with length and capacity
mySlice2 := make([]int, 5, 10) // Length is 5, capacity is 10

// Slice with no size and capacity
mySlice3 := []int{} // Instantiating the slice with an empty array

In Golang, the make function is crucial for creating slices. It allows you to define both the length (number of elements) and the capacity (potential size without reallocating memory).

Adding and Removing Elements

Adding elements to slices in Golang is intuitive. Using the built-in append function, you can grow a slice dynamically. Removing or modifying elements can be achieved with slicing operations.

// Creating a slice
numbers := []int{1, 2, 3, 4, 5}
fmt.Println("Original slice:", numbers) // Output: [1 2 3 4 5]

// Slicing a slice (uses for deletion as well)
subSlice := numbers[1:4]
fmt.Println("Sub-slice:", subSlice) // Output: [2 3 4]

// Appending to a slice
numbers = append(numbers, 6, 7)
fmt.Println("Slice after append:", numbers) // Output: [1 2 3 4 5 6 7]

// Sorting the slice
slices.Sort(numbers)

Maps

Maps, or dictionaries, are key-value data structures that are indispensable in modern programming. Like Java’s HashMap, Golang's map is a flexible way to associate keys with values.

Java Example:

// Initializing HashMap
Map<Integer, Integer> mapObject = new HashMap<>();

// Adding key-value pairs
mapObject.put(1, 1);

// Removing a key
mapObject.remove(1);

// Checking if a key exists
boolean containsKey = mapObject.containsKey(1);

// Looping through the map
for (Map.Entry<Integer, Integer> entry : mapObject.entrySet()) {
Integer key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + ": " + value);
}

Golang Example:

// Initializing the map
mapObject := make(map[int]int)

// Adding key-value pairs
mapObject[1] = 1

// Deleting a key
delete(mapObject, 1)

// Checking if a key exists
value, ok := mapObject[1] // 'ok' is true if the key is present

// Looping through the map
for key, value := range mapObject {
println(key, value)
}

Sets

While Java has a dedicated HashSet class, Golang does not have a built-in set data structure. Instead, developers simulate sets using maps, where the map's key represents the set element, and the value is typically an empty struct (struct{}) to save memory.

Java Example:

// Initializing HashSet
Set<Integer> set = new HashSet<>();

// Adding elements
set.add(1);
set.add(2);

// Removing elements
set.remove(1);

Golang Example:

// Initializing the set
set := make(map[int]struct{})

// Adding elements
set[0] = struct{}{}
set[1] = struct{}{}

// Looping through the set
for key := range set {
println(key)
}

Conclusion

In this article, we’ve explored how Golang handles essential collections — arrays, slices, maps, and sets — and how they compare to their Java counterparts. These data structures are fundamental to writing clean, efficient, and idiomatic code in Golang.

Transitioning from Java to Golang might feel daunting at first, but understanding these foundational elements will make the process much smoother.

Stay tuned for Part III of this series, where we’ll dive into advanced Golang topics, including pointers, channels, and contexts. Mastering these concepts will help you unlock the full potential of Go as a modern programming language.

Happy coding, and see you in the next installment!

--

--

No responses yet