go的map獲取對應的key-value


場景:

 IP是個Key,string字符串是個值, 一個IP可以對應多個字符串。

 

代碼如下:

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)

     }

   
}

 

 運行結果如下:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM