funcclear(ms map[string]interface{})map[string]interface{} { // // Method - I , say book is name of map // for k := range book { // delete(book, k) // }
// // Method - II // book = make(map[string]int)
// // Method - III // book = map[string]int{} ms = make(map[string]interface{}) return ms }
copy()方法
1 2 3 4 5 6
funccopy(dst, src map[string]interface{})map[string]interface{} { for k, v := range src { dst[k] = v } return dst }
haskey()方法
1 2 3 4 5 6 7 8
funchaskey(ms map[string]interface{}, key string)bool { for k := range ms { if k == key { returntrue } } returnfalse }
keys()方法
1 2 3 4 5 6 7
funckeys(ms map[string]interface{}) []string { var keys = []string{} for k := range ms { keys = append(keys, k) } return keys }
values()方法
1 2 3 4 5 6 7
funcvalues(ms map[string]interface{}) []interface{} { var values = []interface{}{} for _, v := range ms { values = append(values, v) } return values }
pop()方法
1 2 3 4 5 6 7
funcpop(ms map[string]interface{}, key string)interface{} { v := ms[key] if v != nil { delete(ms, key) } return v }
彩蛋
1 2 3 4 5 6 7 8 9 10 11
funccalc(x, y int)int { fmt.Println(x, y, x+y) return x + y } funcrun() { a := 1 b := 2 defer calc(a, calc(a, b)) a = 0 defer calc(a, calc(a, b)) }