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?
为什么go.net/websocket'api不使用通道发送和接收消息?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
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
(trygolang-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.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 python 3des pyDes库
- ¥15 关于#mysql#安装失败的问题。MySQL
- ¥15 想问一下for循环计算表达式的方法,第一次接触
- ¥15 如何在VA框架上面加功能,去读取框架内任何app数据功能
- ¥15 关于#c语言#的问题:用c或c++写一个计算下列问题有关软件工程的代码并加上分析
- ¥15 Zeppelin0.10.0版本升级lib包下的shiro-web
- ¥15 链表入队的指针内存问题
- ¥20 vba如何写本地html文件执行js
- ¥15 VS2022的C#如何创建
- ¥20 关于#用户注册#的问题,如何解决?