该TryCloseNormally函数从服务器端关闭 websocket 连接。该函数有两个版本。第一个仅发送关闭请求。第二个正在等待响应——来自客户端的副本。这些函数中哪个是正确的?
版本1
func TryCloseNormally(wsConn *websocket.Conn) error {
closeNormalClosure := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
if err := wsConn.WriteControl(websocket.CloseMessage, closeNormalClosure, time.Now().Add(time.Second)); err != nil {
return err
}
return wsConn.Close()
}
Run Code Online (Sandbox Code Playgroud)
版本2
// The function tries to close the connection according to the RFC 6455 standard.
// The function ALWAYS closes the connection.
//
// To handle the case where the peer sent a data message before the sending the close message,
// function read and discard data messages until …Run Code Online (Sandbox Code Playgroud) 我读到 Golang 语言以一种智能的方式管理内存。使用逃逸分析,go在调用new时可能不会分配内存,反之亦然。golang可以用这样的表示法分配内存吗var bob * Person = & Person {2, 3}?或者指针总是指向堆栈