Maps in Go
A map is a very useful data type in Go.
In other language it’s also called dictionary or hash map or associative array.
Here’s how you create a map:
agesMap := make(map[string]int)
You don’t need to set how many items the map will hold.
You can add a new item to the map in this way:
agesMap["flavio"] = 39
You can also initialize the map with values directly using this syntax:
agesMap := map[string]int{"flavio": 39}
You can get the value associated with a key using:
age := agesMap["flavio"]
You can delete an item from the map using the delete()
function in this way:
delete(agesMap, "flavio")
→ Get my Go Handbook
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025