map---

  • 声明一个map  var my map[int]int   这个map 是nil ,值为  map[],长度为0  ,hash[key]=value 赋值报错,单允许访问
  • 声明map  make方式 var my make(map[T]T,num) ,num为初始map长度,默认为0
  • map的key 不能是function,slice,map,可以是bool,数字,string,指针,channer,interface,struct,array   in short  可以用==判断
    As mentioned earlier, map keys may be of any type that is comparable. The language spec defines this precisely, but in short, comparable types are boolean, numeric, string, pointer, channel, and interface types, and structs or arrays that contain only those types. Notably absent from the list are slices, maps, and functions; these types cannot be compared using ==, and may not be used as map keys.
func main() {
	// Declare and make a map that stores values
	// of type user with a key of type string.
	users := make(map[string]user)

	// Add key/value pairs to the map.
	users["Rob"] = user{"Roy", "Rob"}
	users["Ford"] = user{"Henry", "Ford"}
	users["Mouse"] = user{"Mickey", "Mouse"}
	users["Jackson"] = user{"Michael", "Jackson"}

	// Iterate over the map.
	for key, value := range users {
		fmt.Println(key, value)
	}

	fmt.Println()

	// Iterate over the map and notice the
	// results are different.
	for key := range users {
		fmt.Println(key)
	}
}
// Sample program to show how to declare and initialize a map
// using a map literal.
type user struct {
	name  string
	email string
}

// main is the entry point for the application.
func main() {
	// Declare and initialize the map with values.
	users := map[string]user{
		"Rob":     user{"Roy", "Rob"},
		"Ford":    user{"Henry", "Ford"},
		"Mouse":   user{"Mickey", "Mouse"},
		"Jackson": user{"Michael", "Jackson"},
	}

	// Iterate over the map.
	for key, value := range users {
		fmt.Println(key, value)
	}
}
Sample program to show how to compose maps of maps.
// Data defines a key/value store.
type Data map[string]string
Sample program to show how to compose maps of maps.
// main is the entry point for the application.
func main() {
	// Declare and make a map of Data type values.
	users := make(map[string]Data)

	// Initialize some data into our map of maps.
	users["clients"] = Data{"123": "Henry", "654": "Joan"}
	users["admins"] = Data{"398": "Bill", "076": "Steve"}

	// Iterate over the map of maps.
	for key, data := range users {
		fmt.Println(key)
		for key, value := range data {
			fmt.Println("\t", key, value)
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值