> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/10-map.md).

# Map

Let's break down the provided Go code step by step:

```go
package main

import (
    "fmt"
    "maps"
)
```

* The code begins with the package declaration. In this case, it's a standalone program with the `main` package.
* It imports two packages, "fmt" (for formatting and printing) and a custom package "maps."

```go
func main() {
    // Create an empty map with string keys and int values
    m := make(map[string]int)

    // Assign values to keys in the map
    m["k1"] = 7
    m["k2"] = 13

    // Print the entire map
    fmt.Println("map:", m)

    // Retrieve and print the value associated with the key "k1"
    v1 := m["k1"]
    fmt.Println("v1:", v1)

    // Try to retrieve a value for a non-existent key "k3"
    v3 := m["k3"]
    fmt.Println("v3:", v3)

    // Print the length of the map
    fmt.Println("len:", len(m))

    // Delete the key "k2" from the map
    delete(m, "k2")
    fmt.Println("map:", m)

    // Clear the entire map
    clear(m)
    fmt.Println("map:", m)

    // Check if the key "k2" exists in the map using a blank identifier (_)
    _, prs := m["k2"]
    fmt.Println("prs:", prs)

    // Create and print a new map using a shorthand syntax
    n := map[string]int{"foo": 1, "bar": 2}
    fmt.Println("map:", n)

    // Create another map with the same content as "n" and check if they are equal
    n2 := map[string]int{"foo": 1, "bar": 2}
    if maps.Equal(n, n2) {
        fmt.Println("n == n2")
    }
}
```

* The `main` function is where the program execution begins.
* A map `m` is created with string keys and int values using the `make` function.
* Key-value pairs are added to the map using the assignment operator (`=`).
* The map is printed using `fmt.Println`.
* The value associated with the key "k1" is retrieved and printed.
* An attempt to retrieve a value for a non-existent key "k3" is made, and the result is printed.
* The length of the map is printed using `len`.
* The key "k2" is deleted from the map using the `delete` function.
* The `clear` function is called to clear the entire map (Note: `clear` function should be defined somewhere in your code, but it's not provided in the snippet).
* The existence of the key "k2" is checked using a blank identifier.
* Another map `n` is created using a shorthand syntax and printed.
* Another map `n2` is created with the same content as `n`, and their equality is checked using a custom function `maps.Equal`.

Please note that the `clear` function is not defined in the provided code snippet. If you intend to use it, you need to define it in your code. Additionally, the custom package "maps" is imported, but its functions are not provided in the snippet. Ensure that the necessary functions from the "maps" package are available in your code for the program to work correctly.

### Output

```
map: map[k1:7 k2:13]
v1: 7
v3: 0
len: 2
map: map[k1:7]
map: map[]
prs: false
map: map[bar:2 foo:1]
n == n2
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/10-map.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
