dptt66700 2013-08-30 08:36
浏览 63


go.net/websocket package has Read() and Write() functions for sending and receiving messages through the web socket. Why does not it instead return a channel for sending and receiving messages? I feel like packages such as websocket, net are perfect places to use go's channels. What is the reasoning behind this design decision?

  • 写回答

1条回答 默认 最新

  • duanhongyi2964 2013-08-30 08:50

    I don't know the exact semantics of WebSockets, but in general, I don't think network sockets map well into channels. There was actually a netchan package that tried to did this with channels in general, but it was discontinued.

    I think there many problems to try and support a whole lot of protocols with one channel implementation. Where does the message begin and end? Should the channel buffer a large message, or give it chunk by chunk, etc.? The semantics of each protocol differ too much, so Go gives you the lower-level Read/Write for sockets, just like you'd find in other languages and leaves it for you to decide how to handle the data.

    Note that I'm talking about socket-channels in general. WebSocket is a well-defined protocol and an implementation using channels might work well for it. As for why channels weren't chosen, it would be best to ask the authors of go.net/websocket (try golang-nuts Google group). I think it makes sense in a way, as its API as it is now is similar to the regular Go socket API.

    Note that not using channels does not make the API non-concurrent. As long as connections are being handled on separate goroutines (which they are, with Go's HTTP server), they'll be handled concurrently. Using a channel or not is just a matter of convenience here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件