# map

使用方式:

```go
test := make(map[int]string)

test := map[int]string{
    1: "a",
    2: "b",
    3: "c",
}
```

map 跟 struct 最大的差別在於:

```
map 的所有 key 要同型別，所有的value 也要同型別
```

## 新增key

```go
test[123] = "as"
```

## 刪除key

```go
delete (test, 123)
```

## 查詢key

```go
test[123]
```

## iterate Map 的 value

```go
package main

import (
	"fmt"
)

func main() {
	test := map[string]int{
		"jason": 12,
		"eason": 15,
	}
	for idx := range test {
		fmt.Println(test[idx])
	}
}
```

### 巢狀 Nested Map

```go
package main

import (
	"fmt"
)

func main() {
	var test = map[string]map[string]int{
		"a": map[string]int{}, // 記得要逗點
	}

	test["a"]["w"] = 123
	fmt.Println(test)
}

```

<https://stackoverflow.com/a/44305711/4622645>


---

# Agent Instructions: 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:

```
GET https://easonwang.gitbook.io/golang/ji-ben-yu-fa/map.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
