DEV Community

Cover image for Understanding Maps in Go
Ganesh Kumar
Ganesh Kumar

Posted on

Understanding Maps in Go

#go

Hello, I'm Ganesh Kumar. I'm working on git-lrc: a Git hook for Checking AI generated code.AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

In my previous post, I explained how to loop over a slice using normal index based loop and range function.

In this post, I will explain what are maps and how to create them.

Why maps are used?

Go offers a very simple implementation of hash maps where you can call it has dictionary or hash table.

The typical structure of a map is:

map[keyType]valueType
Enter fullscreen mode Exit fullscreen mode

Where keyType is the type of the key and valueType is the type of the value.

keyType mostly be a string, but we can also use other data types as a key.

keyType must be a type that can be compared using the == operator.

Whenever we want to access a value quickly, or modify a value, we use the maps.

How to create maps

let's see how to create a map using make function and litral.

For the example let's consider a map of pc name and price in dollars.

  1. Create a map using make function

Assign the map to a variable and then assign the values to it.


package main

import "fmt"


func main() {
    mymap := make(map[string]int)
    mymap["hp"] = 1000
    mymap["dell"] = 800
    mymap["lenovo"] = 600
    fmt.Println(mymap)
    fmt.Println(mymap["hp"])

}
Enter fullscreen mode Exit fullscreen mode

Output:

map[dell:800 hp:1000 lenovo:600]
1000
Enter fullscreen mode Exit fullscreen mode

Here we created a map using make function and assigned the values to it.

  1. create a map using litral

Assign the map to a variable and then assign the values to it.

package main

import "fmt"


func main() {
    mymap := map[string]int{}
    mymap["hp"] = 1000
    mymap["dell"] = 800
    mymap["lenovo"] = 600
    fmt.Println(mymap)
    fmt.Println(mymap["hp"])

}

Enter fullscreen mode Exit fullscreen mode

Output:

map[dell:800 hp:1000 lenovo:600]
1000
Enter fullscreen mode Exit fullscreen mode

We can see that the map is created and the values are stored in it.

Conclusion

We understood what are maps and how to create them.

git-lrc

πŸ‘‰ Check out: git-lrc
Any feedback or contributors are welcome! It’s online, open-source, and ready for anyone to use.
⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • πŸ€– AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • πŸ” Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • πŸ” Build a habit, ship better code. Regular review β†’ fewer bugs β†’ more robust code β†’ better results in your team.
  • πŸ”— Why git? Git is universal. Every editor, every IDE, every AI…




https://github.com/golang/go/blob/master/src/runtime/slice.go

Top comments (0)