Welcome to the world of Go maps Navigating! If you’re new to Go and want to understand how to use maps effectively, you’re in the right place. Maps, a built-in data type in Go, are incredibly useful for organizing and finding data within collections. In this guide, we’ll cover the basics of declaring, initializing, and working with maps in Go.
Declaring and Initializing Maps in Go
Maps in Go are collections that link one value (the key) to another value. For example, you can use a map to associate a student’s name (the key) with their grade (the value). Declaring and initializing a map is straightforward. You can use the make function or a composite literal. Here’s how:
// Using the make function
var grades1 = make(map[string]int)
// Using composite literal syntax
var grades2 = map[string]int{}
// Pre-populated map using composite literal syntax
var grades3 = map[string]int{"John": 85, "Jane": 90}
In the above examples:
- Keys are strings (e.g.,
"John"), and - Values are integers (e.g.,
85).
Key Points:
- Keys and values can be of any comparable type.
- Composite literals allow you to initialize maps with pre-defined key-value pairs.
Working with Map Elements
Go maps Navigating provide efficient ways to manipulate their elements. You can add, update, retrieve, and delete entries with ease. Here’s a step-by-step guide:
var grades = make(map[string]int)
grades["John"] = 85 // Add John's grade
grades["Jane"] = 90 // Add Jane's grade
johnGrade := grades["John"] // Retrieve John's grade
grades["John"] = 95 // Update John's grade
delete(grades, "John") // Delete John's entry
johnGrade, johnExists := grades["John"] // Check if John's grade exists
if johnExists {
fmt.Println(johnGrade) // Won't execute, as John was deleted
}
Key Points:
- Use the key (e.g.,
"John") to perform operations. - The
deletefunction removes an entry from the map. - The
okpattern (johnGrade, johnExists := grades["John"]) checks if a key exists in the map.
Maps and Reference Types
Maps in Go are reference types. This means that when you assign a map to a new variable, you create a reference to the original map, not a copy. Here’s an example to illustrate this:
var originalData = map[string]int{"apple": 1, "banana": 2}
var copiedData = originalData
copiedData["apple"] = 100
fmt.Println(originalData) // Reflects updated value: 100
fmt.Println(copiedData) // Also reflects updated value: 100
Key Takeaway:
Changes made to copiedData affect originalData because both variables point to the same underlying data.
Lesson Summary and Practice
Congratulations! You’ve learned the following about Go maps:
- How to declare and initialize maps.
- How to manipulate map elements (add, update, delete, retrieve).
- How maps behave as reference types.
What’s Next?
Now, it’s time to solidify your knowledge with practice exercises. Explore Go’s documentation and write your own code snippets to reinforce these concepts. You can also check out the official Go documentation for more advanced topics and examples.
Happy coding!
Discover more from teguhteja.id
Subscribe to get the latest posts sent to your email.

