What's a Map?

main.go
package main
import "fmt"
func main() {
colors := map[string]string{
"red": "#ff0000",
"green": "#4bf745",
}
fmt.Println(colors)
}

Manipulating Maps

Iterating Over Maps


package main
import (
"fmt"
)
func main() {
colors := map[string]string{
"red": "#ff0000",
"green": "#4bf745",
"white": "#ffffff",
}
printMap(colors)
}
func printMap(c map[string]string) {
for color, hex := range c {
fmt.Println("Hex code for", color, "is", hex)
}
}
Differences Between Maps and Structs

Quiz 9: Test Your Knowledges:Maps



文章介绍了Go语言中Map的数据结构,展示了如何初始化和遍历Map,以及与Structs的区别。通过示例代码,解释了如何打印Map中的键值对,强调了Map在存储和访问数据时的特性。
1807

被折叠的 条评论
为什么被折叠?



