```
type natmap struct {
sync.RWMutex
m map[string][]net.PacketConn
timeout time.Duration
}
func newNATmap(timeout time.Duration) *natmap {
m := &natmap{}
m.m = make(map[string][10]net.PacketConn)
log.Printf("%#v", m)
m.timeout = timeout
return m
}
```
m.m = make(map[string][10]net.PacketConn)
关键是这句,想在初始化的时候初始化一个10个空间的数组包含在字典里面,不然运行时报错超出数组长度.
求大佬帮忙
```
type natmap struct {
sync.RWMutex
m map[string][]net.PacketConn
timeout time.Duration
}
func newNATmap(timeout time.Duration) *natmap {
m := &natmap{}
m.m = make(map[string][]net.PacketConn)
log.Println(m.m)
m.timeout = timeout
return m
}
func addConn(n *natmap, name string, conn net.PacketConn) {
pool := n.m[name]
if len(pool) == 0 {
pool = make([]net.PacketConn, 0, 10)
}
n.m[name] = append(pool, conn)
}
```
可以试下,在添加连接的时候,再去创建map中的key对应的切片
#4
更多评论