DEV Community

Cover image for Mastering Collections in Go: A Comprehensive Guide to Arrays, Slices, and Maps
Avinash Chodavarapu
Avinash Chodavarapu

Posted on • Edited on

Mastering Collections in Go: A Comprehensive Guide to Arrays, Slices, and Maps

Go, a statically-typed and compiled programming language created at Google, is known for its simplicity, efficiency, and strong support for concurrent programming. In this blog, we will explore three built-in data structures in Go that allow you to store and manage collections of values: arrays, slices, and maps.

  1. Arrays: An array is a fixed-size, ordered collection of elements of the same type. To declare an array, you need to specify the element type and the size of the array.

Syntax:


var array_name [size]element_type

Enter fullscreen mode Exit fullscreen mode

Example:


var numbers [5]int // Declares an array of integers with a fixed size of 5

var numbers [5]int = [5]int{1, 2, 3, 4, 5} // Initializes an array of integers with a fixed size of 5
                                 (or)

numbers := [5]int{1, 2, 3, 4, 5} // Declares and initializes an array 
Enter fullscreen mode Exit fullscreen mode

To access and modify elements in an array, use the array index. Indices start at 0.


fmt.Println(numbers[0]) // Output: 1

Enter fullscreen mode Exit fullscreen mode
  1. Slices: Slices are more flexible and powerful than arrays. A slice is a dynamically-sized, ordered collection of elements of the same type. It is built on top of an array but provides a dynamic size and additional built-in functions.

Declaring a slice:


var slice_name []element_type

Enter fullscreen mode Exit fullscreen mode

Example:


var names []string // Declares a slice of strings with no initial size

Enter fullscreen mode Exit fullscreen mode

To create a slice with initial elements, use the slice literal syntax:


fruits := []string{"apple", "banana", "orange"}

Enter fullscreen mode Exit fullscreen mode

Slices can be manipulated using built-in functions like make, len, cap, and append.

  1. Maps: Maps are unordered collections of key-value pairs, where each key is unique. They can be thought of as hash tables or dictionaries in other languages.

Declaring a map:


var map_name map[key_type]value_type

Enter fullscreen mode Exit fullscreen mode

Example:


var grades map[string]int // Declares a map with string keys and integer values

Enter fullscreen mode Exit fullscreen mode

To create a map with initial elements, use the map literal syntax:


ages := map[string]int{
    "Alice": 30,
    "Bob":   25,
}

Enter fullscreen mode Exit fullscreen mode
  1. Manipulating maps:
  • Adding a key-value pair: map_name[key] = value
  • Accessing a value: value := map_name[key]
  • Deleting a key-value pair: delete(map_name, key)

Go provides powerful built-in data structures to work with collections of values. Arrays offer fixed-size collections, while slices provide a more flexible and dynamic alternative. Maps are unordered key-value pairs that are useful for organizing data. By understanding the basics of these data structures, you can effectively store, manage, and manipulate data in your Go programs.

Top comments (0)