#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Data Structures

How to Use Maps in Go – Best Practices for Key-Value Data

What is a Map?

A map is Go's built-in hash table type, storing unordered key-value pairs.

Declaring and Using Maps

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

ages["Charlie"] = 35        // add a new key
fmt.Println(ages["Alice"])  // 25

value, exists := ages["Dave"]
if !exists {
    fmt.Println("Dave not found")
}

delete(ages, "Bob")

Best Practices

  • Always check for existence using the two-value form (value, ok := map[key]) to distinguish a missing key from a zero value.
  • Maps in Go are unordered — never rely on iteration order when looping with for key, value := range map.
  • Use make(map[string]int) to initialize an empty map before adding keys, to avoid a nil map panic on write.

Ready to master real-world Go skills?

Learn Go hands-on with mentor-led, live sessions.

Explore Course