内容纲要

map 的初始化方式

方式一

// Declares a nil map
var chapts map[int]string

// Initialize map with make function
chapts = make(map[int]string)

// Add data as key/value pairs
chapts[1] = "Beginning Go"
chapts[2] = "Go Fundamentals"
chapts[3] = "Structs and Interfaces"

方式二

// Declare and initialize map using map literal
langs := map[string]string{
    "EL": "Greek",
    "EN": "English",
    "ES": "Spanish",
    "FR": "French",
    "HI": "Hindi",
}

创建一个空 map

var m = map[string]int{}
package main

import (
    "fmt"
)

func main() {
    // Declares a nil map
    var chapts map[int]string

    // Initialize map with make function
    chapts = make(map[int]string)

    // Add data as key/value pairs
    chapts[1] = "Beginning Go"
    chapts[2] = "Go Fundamentals"
    chapts[3] = "Structs and Interfaces"

    // Iterate over the elements of map using range
    for k, v := range chapts {
        fmt.Printf("Key: %d Value: %s\n", k, v)
    }

    // Declare and initialize map using map literal
    langs := map[string]string{
        "EL": "Greek",
        "EN": "English",
        "ES": "Spanish",
        "FR": "French",
        "HI": "Hindi",
    }

    // Delete an element
    delete(langs, "EL")

    // Lookout an element with key
    if lan, ok := langs["EL"]; ok {
        fmt.Println(lan)
    } else {
        fmt.Println("\nKey doesn't exists")
    }
    // Passing a map to function doesn't make a copy
    removeLan(langs, "HI")
    for k, v := range langs {
        fmt.Printf("Key: %s Value: %s\n", k, v)
    }
}
func removeLan(langs map[string]string, key string) {
    delete(langs, key)
}

排序

package main

import (
    "fmt"
    "sort"
)

func main() {
    // Initialize map with make function
    chapts := make(map[int]string)

    // Add data as key/value pairs
    chapts[1] = "Beginning Go"
    chapts[2] = "Go Fundamentals"
    chapts[3] = "Structs and Interfaces"
    for k, v := range chapts {
        fmt.Println(k, v)
    }

    // Slice for specifying the order of the map
    var keys []int
    // Appending keys of the map
    for k := range chapts {
        keys = append(keys, k)
    }
    // Ints sorts a slice of ints in increasing order.
    sort.Ints(keys)
    // Iterate over the map with an order
    for _, k := range keys {
        fmt.Println("Key:", k, "Value:", chapts[k])
    }
}
package main

import (
   "fmt"
   "sync"
)

func main() {
   var mapInt = new(sync.Map)
   //add elem
   mapInt.Store(1, 1)
   mapInt.Store(2, 2)
   mapInt.Store(3, 3)
   fmt.Println("before delete key:")
   // iterator
   mapInt.Range(func(key, value interface{}) bool {
      fmt.Println(key, value)
      return true
   })
   // del
   mapInt.Delete(2)
   fmt.Println("after delete key:")
   mapInt.Range(func(key, value interface{}) bool {
      fmt.Println(key, value)
      return true
   })
   // query
   v, ok := mapInt.Load(1)
   if ok {
      fmt.Println(v)
   }
   // load or store
   v, ok = mapInt.LoadOrStore(2, 10)
   fmt.Println("load or store:", v, ", ok:", ok)

}
output:
before delete key:
1 1
2 2
3 3
after delete key:
1 1
3 3
1
load or store: 10 , ok: false

发表评论

电子邮件地址不会被公开。 必填项已用*标注