ipTable类型是sync.Map,key全部是IP地址,value全部是true,长度未知,现在需要提取所有IP地址赋值给一个变量,每个IP以逗号间隔
package main
import (
"fmt"
"sync"
)
func main() {
var ipTable sync.Map
ipTable.Store("10.0.0.1", true)
ipTable.Store("10.0.0.2", true)
ipTable.Store("10.0.0.3", true)
ipTable.Store("10.0.0.4", true)
val := ""
flag := true
ipTable.Range(func(key, value interface{}) bool {
if flag {
flag = false
} else {
val += ","
}
val += key.(string)
return true
})
fmt.Println(val)
}
package main
import (
"fmt"
"sync"
)
func main() {
var scene sync.Map
// 将键值对保存到sync.Map
scene.Store("greece", 97)
scene.Store("london", 100)
scene.Store("egypt", 200)
// 从sync.Map中根据键取值
fmt.Println(scene.Load("london"))
// 根据键删除对应的键值对
scene.Delete("london")
// 遍历所有sync.Map中的键值对
scene.Range(func(k, v interface{}) bool {
fmt.Println("iterate:", k, v)
return true
})
}
ppsedruat fgurhyiw?.
Gsnsnsnsns
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!