go的map獲取對應的key-value

場景:app

 IP是個Key,string字符串是個值, 一個IP能夠對應多個字符串。spa

 

代碼以下:code

package main

import (
    "fmt"
)


func main() {
   tokens := []string{"abc", "cde", "23243423", "werwer", "sdfjsidfjisdjf"}
   ip := []string{"192.168.0.1", "192.168.0.2", "192.168.0.3"}
   serverIPMap := make(map[string][]string)
   for i, token := range tokens {
      ipValue := ip[i%3]
      serverIPMap[ipValue] = append(serverIPMap[ipValue], token)
   }

   fmt.Println(serverIPMap)

        for key, iptokens :=  range serverIPMap{
                  
                     fmt.Println("key",key)
                      fmt.Println("value",iptokens)

     }

   
}

 

 運行結果以下:server

相關文章
相關標籤/搜索