<p>Hi there gophers :)</p>
<p>I've uploaded my first Go project on Github and I would really like some feedback. I'm still fairly new to the language but I'm already in love with it!!</p>
<p>Github: <a href="https://github.com/tabone/websocket" rel="nofollow">https://github.com/tabone/websocket</a></p>
<p>Thanks in advance!!
taboneio</p>
<hr/>**评论:**<br/><br/>kardianos: <pre><p>I'm glad you like go!</p>
<p>Have you seen godoc.org?
<a href="https://godoc.org/?q=websocket" rel="nofollow">https://godoc.org/?q=websocket</a></p>
<p>The two most used I think are:</p>
<ul>
<li><a href="https://godoc.org/github.com/gorilla/websocket" rel="nofollow">https://godoc.org/github.com/gorilla/websocket</a></li>
<li><a href="https://godoc.org/golang.org/x/net/websocket" rel="nofollow">https://godoc.org/golang.org/x/net/websocket</a></li>
</ul>
<p>Just a skim, but I would prefer "//" to "/* */" for struct field docs.
Also "func (s *Socket) Write(o int, p []byte) error" prevents using something like io.Copy on it. Maybe set that elsewhere?</p>
<p>You might also want to type your constants.
"type Op int"
"const OpText Op = 1"</p></pre>taboneIO: <pre><p>Hi kardianos,</p>
<p>Thanks for taking the time to give me some tips!!</p>
<p>To tell you the truth, initially I used /**/ but for some reason 'goreportcard' was having some problems parsing the docs.</p>
<p>Regarding the Write() method, I would like to make it implement the io.Writer interface however since a frame can take different forms (ex. Text, Binary, Ping, etc...), the opcode its kind of required. Maybe I should have picked a better method name.</p>
<p>Regarding your third tip (Type Constants) is there a good reason to do it? (sorry for asking :s I don't want to sound rude). By using built in types makes the process of sending a message easier and shorter.</p>
<p>Thanks for your time!</p></pre>kardianos: <pre><p>Write: set it on your stream (set mode), then remove from write.</p>
<p>Constants: programmer documentation, prevent from making silly mistakes.</p></pre>synalx: <pre><p>So, for the <code>Write()</code> method, I agree that a better method name would be ideal. Maybe <code>WriteMessage()</code>? It's definitely strange to see a method with that name that doesn't satisfy the <code>Writer</code> interface.</p>
<p>Here's one idea on how to do this:</p>
<pre><code>type SocketBinaryWriter struct {
socket *Socket
}
func (sbw SocketBinaryWriter) Write(payload []byte) (n int, err error {
return sbw.socket.WriteMessage(OpcodeBinary, payload)
}
func (s *Socket) BinaryWriter() SocketBinaryWriter {
return SocketBinaryWriter{s}
}
</code></pre>
<p>This way you can get an <code>io.Writer</code> for writing binary data to the <code>Socket</code>, but also expose the lower level interface for other opcode types (which could have their own wrappers where appropriate).</p>
<p>Typed constants: it's very important that you do this. Seeing (o int) I have no idea what is valid to pass, but seeing (o Opcode) I know instantly to look for Opcode* constants of the right type.</p></pre>mwholt: <pre><p>Props for implementing a protocol. That's great practice and a super important skill to have!</p></pre>
My First Project - The WebSocket Protocol - Would really appreciate some feedback
xuanbao · · 486 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传