Tutorials Logic, IN info@tutorialslogic.com

Golang Arrays, Slices and Maps: Collections with Examples

Arrays vs Slices

Go has arrays, slices, and maps for working with collections. Arrays have fixed length, slices are flexible views over arrays, and maps store key-value pairs for fast lookup.

Most Go programs use slices more often than arrays. Slices can grow with `append`, can be ranged over, and are passed around as small descriptors pointing to underlying array storage. Maps are ideal when values are found by key, such as user ID, slug, email, or status.

An array length is part of its type, so `[3]int` and `[4]int` are different types. A slice type like `[]int` is more flexible and can represent any number of integers.

  • Use arrays for fixed-size data where length is part of the model.
  • Use slices for most lists and dynamic collections.
  • `append` may allocate a new underlying array when capacity is exceeded.
  • A slice has length and capacity; length is current items, capacity is available backing storage.

Slice Basics

Slice Basics
package main

import "fmt"

func main() {
	scores := []int{70, 85, 90}
	scores = append(scores, 95)

	for index, score := range scores {
		fmt.Println(index, score)
	}

	fmt.Println("length:", len(scores))
	fmt.Println("capacity:", cap(scores))
}

Maps

A map stores values by key. The key type must be comparable, such as string, int, bool, or a struct made of comparable fields. Maps are reference-like values, so changes through one map variable are visible through another variable pointing to the same map.

  • Create maps with a literal or with `make`.
  • Use comma-ok lookup to distinguish missing keys from zero values.
  • Delete entries with `delete(mapValue, key)`.
  • Map iteration order is intentionally not guaranteed.

Map Lookup

Map Lookup
package main

import "fmt"

func main() {
	stock := map[string]int{
		"keyboard": 10,
		"mouse":    0,
	}

	quantity, ok := stock["mouse"]
	if ok {
		fmt.Println("mouse quantity:", quantity)
	}

	if _, ok := stock["monitor"]; !ok {
		fmt.Println("monitor is not in the map")
	}
}

Collection Design Tips

Choose slices when order matters, duplicates are allowed, or you need to process every item. Choose maps when lookup by key is the main operation.

  • Use a slice of structs for ordered records.
  • Use a map for fast lookup by ID or name.
  • Combine both when you need stable order and fast lookup.
  • Avoid modifying a slice while ranging over it unless the behavior is deliberate and tested.

Slice Plus Map Index

Slice Plus Map Index
type Product struct {
	ID    int
	Name  string
	Price int
}

products := []Product{
	{ID: 1, Name: "Keyboard", Price: 1200},
	{ID: 2, Name: "Mouse", Price: 500},
}

byID := make(map[int]Product)
for _, product := range products {
	byID[product.ID] = product
}
Before you move on

Golang Arrays, Slices and Maps: Collections with Examples Mastery Check

5 checks
  • An array length is part of its type, so `[3]int` and `[4]int` are different types.
  • A slice type like `[]int` is more flexible and can represent any number of integers.
  • The key type must be comparable, such as string, int, bool, or a struct made of comparable fields.
  • Maps are reference-like values, so changes through one map variable are visible through another variable pointing to the same map.
  • Choose slices when order matters, duplicates are allowed, or you need to process every item.

Golang Arrays, Slices and Maps Questions Learners Ask

Slices are flexible and work for most dynamic lists. Arrays are fixed-size and less common in everyday Go code.

No. Map keys must be comparable, and slices are not comparable.

A slice is a small descriptor pointing at an underlying array. If two slices share that array, changing elements through one slice can be visible through the other. append may either reuse the same array or allocate a new one depending on capacity, which is why the behavior can surprise beginners.

Next Step
Next Practice

Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.