Sling Academy
Home/Golang/Passing Maps to Functions in Go: Reference vs Copy

Passing Maps to Functions in Go: Reference vs Copy

Last updated: November 24, 2024

In Go, maps are an important data structure used for storing key-value pairs. They are highly versatile and can be passed to functions efficiently. However, it is important to understand whether maps in Go are passed by reference or by value. This article explores how maps are passed to functions and the implications of each approach.

Understanding Maps in Go

Maps in Go are similar to dictionaries or hash tables in other languages. They allow you to store and retrieve values using unique keys. Here's a basic example of a map in Go:


package main

import "fmt"

func main() {
    colors := map[string]string{
        "red":   "#FF0000",
        "green": "#00FF00",
        "blue":  "#0000FF",
    }
    fmt.Println(colors)
}

Passing Maps to Functions: Reference vs Copy

When you pass a map to a function in Go, you are passing a reference to the map, not a copy of the map. This means changes made to the map within the function will affect the map outside the function as well. Let's see an example to illustrate this:


package main

import "fmt"

func updateColor(colors map[string]string) {
    colors["red"] = "#FF1111"
}

func main() {
    colors := map[string]string{
        "red":   "#FF0000",
        "green": "#00FF00",
        "blue":  "#0000FF",
    }
    updateColor(colors)
    fmt.Println(colors)
    // Output: map[red:#FF1111 green:#00FF00 blue:#0000FF]
}

In the above example, the map colors is passed to updateColor, which modifies the "red" key's value. Since maps are passed by reference, the changes are reflected in the colors map in the main function as well.

Intermediate Example: Using Map Functions

Now, let's create a function that adds a new color to the map. This example illustrates how flexibility is maintained when using maps across various functions:


package main

import "fmt"

func addColor(colors map[string]string, colorName string, hex string) {
    colors[colorName] = hex
}

func main() {
    colors := map[string]string{
        "red":   "#FF0000",
        "green": "#00FF00",
    }
    addColor(colors, "blue", "#0000FF")
    fmt.Println(colors)
    // Output: map[red:#FF0000 green:#00FF00 blue:#0000FF]
}

Advanced Example: Maps with Nested Maps

You can have maps as values within other maps. This allows for complex data structures. Let's see how to handle nested maps by defining a function that operates on such structures:


package main

import "fmt"

type ColorDetails struct {
    Name string
    Hex  string
}

func updateShade(m map[string]map[string]string, primaryColor, newShade, newHex string) {
    shades, exists := m[primaryColor]
    if exists {
        shades[newShade] = newHex
    }
}

func main() {
    colors := map[string]map[string]string{
        "red": {
            "darkRed": "#8B0000",
            "lightRed": "#FF6347",
        },
        "green": {
            "darkGreen": "#006400",
            "lightGreen": "#90EE90",
        },
    }
    updateShade(colors, "red", "mediumRed", "#CD5C5C")
    fmt.Println(colors[
    // Output: map[red:map[darkRed:#8B0000 lightRed:#FF6347 mediumRed:#CD5C5C] green:map[darkGreen:#006400 lightGreen:#90EE90]]
}

In this advanced example, the updateShade function adds a new shade to the color map. Since maps are passed as references, nested maps receive updates without needing to return them from the function.

Conclusion

Understanding how maps are passed to functions in Go is crucial for efficient and error-free use of this data structure. Remember, in Go, maps are passed by reference, meaning changes to maps within functions reflect in the calling context. This tutorial has covered basic, intermediate, and advanced concepts to provide a comprehensive understanding of operating with maps in Go.

Next Article: How to Sort Keys in Go Maps

Previous Article: Maps vs Arrays vs Slices in Go: When to Use Which

Series: Working with Maps in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant