我有一台服务器,我不想将每个连接都保存到一个列表中。可以说:

type Connection struct {
   Id uint16
   Conn *conn.TCP
}

var connections []Connection

但是我想删除/获取特定的连接ID是什么?我应该使用什么?

我在想类似的东西:
func GetConnectionById(id uint16) Connection {
    for k, v := range connections {
       if v.Id == id {
          return v
       }
    }
}

有没有更好的方法?

最佳答案

为什么不通过Connection识别 map 中的每个Id

package main

type Connection struct {
   Id uint16
   X string
}

var connections map[uint16]Connection

func main() {
    connections = make(map[uint16]Connection)
    connections[1] = Connection{}
}

07-26 09:29
查看更多