codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

13 / 72 topics
11Arrays12Slices13Maps14Structs15Methods16Interfaces
Tutorials/Go (Golang)/Maps
🐹Go (Golang)

Maps

Updated 2026-05-15
10 min read

Maps

Introduction

In Go, a map is an unordered collection of key-value pairs. Maps are reference types, which means that when you assign one map to another, both variables point to the same underlying data structure. This makes maps highly efficient for lookups, insertions, and deletions.

Maps are particularly useful when you need to associate unique keys with values. For example, you might use a map to store user IDs as keys and their corresponding usernames as values.

Concept

A map in Go is declared using the map keyword followed by the key type and value type in square brackets. Here's the basic syntax:

var myMap map[keyType]valueType

You can also initialize a map with make function:

myMap := make(map[keyType]valueType)

Maps are not initialized by default, so you need to use make or assign a pre-initialized map.

Examples

Creating a Map

Let's create a simple map that associates integer keys with string values:

Go
1package main
2
3import "fmt"
4
5func main() {
6 // Declare and initialize a map
7 myMap := make(map[int]string)
8
9 // Assign values to the map
10 myMap[1] = "Alice"
11 myMap[2] = "Bob"
12 myMap[3] = "Charlie"
13
14 fmt.Println(myMap)
15}
Output
map[1:Alice 2:Bob 3:Charlie]

Accessing Map Values

You can access the value associated with a key using the syntax myMap[key]. If the key does not exist, it returns the zero value of the value type.

Go
1package main
2
3import "fmt"
4
5func main() {
6 myMap := map[int]string{
7 1: "Alice",
8 2: "Bob",
9 3: "Charlie",
10 }
11
12 // Accessing a value
13 fmt.Println(myMap[2]) // Output: Bob
14
15 // Accessing a non-existent key
16 fmt.Println(myMap[4]) // Output: (empty string)
17}
Output
Bob

Checking for Key Existence

To check if a key exists in the map, you can use a two-value assignment:

value, exists := myMap[key]

If exists is true, then value contains the value associated with the key. If exists is false, then key does not exist in the map.

Go
1package main
2
3import "fmt"
4
5func main() {
6 myMap := map[int]string{
7 1: "Alice",
8 2: "Bob",
9 3: "Charlie",
10 }
11
12 value, exists := myMap[2]
13 if exists {
14 fmt.Println("Key exists:", value)
15 } else {
16 fmt.Println("Key does not exist")
17 }
18}
Output
Key exists: Bob

Deleting a Key-Value Pair

You can delete a key-value pair from the map using the delete function:

delete(myMap, key)

This removes the key and its associated value from the map.

Go
1package main
2
3import "fmt"
4
5func main() {
6 myMap := map[int]string{
7 1: "Alice",
8 2: "Bob",
9 3: "Charlie",
10 }
11
12 delete(myMap, 2)
13 fmt.Println(myMap) // Output: map[1:Alice 3:Charlie]
14}
Output
map[1:Alice 3:Charlie]

Iterating Over a Map

You can iterate over the key-value pairs in a map using a for loop with the range keyword:

Go
1package main
2
3import "fmt"
4
5func main() {
6 myMap := map[int]string{
7 1: "Alice",
8 2: "Bob",
9 3: "Charlie",
10 }
11
12 for key, value := range myMap {
13 fmt.Printf("Key: %d, Value: %s
14", key, value)
15 }
16}
Output
Key: 1, Value: Alice
Key: 2, Value: Bob
Key: 3, Value: Charlie

Map Literals

You can also initialize a map using a map literal:

myMap := map[keyType]valueType{
    key1: value1,
    key2: value2,
    // ...
}

<CodeBlock language="go">
{`package main

import "fmt"

func main() {
    myMap := map[int]string{
        1: "Alice",
        2: "Bob",
        3: "Charlie",
    }

    fmt.Println(myMap)
}`}
</CodeBlock>

<OutputBlock>
{`map[1:Alice 2:Bob 3:Charlie]`}
</OutputBlock>

## What's Next?

In the next section, we will explore structs in Go. Structs are composite data types that allow you to group multiple fields together into a single type.

Stay tuned for more insights into Go's powerful features!

PreviousSlicesNext Structs

Recommended Gear

SlicesStructs