<p>This package was just written to implement JSON according to the Google JSON style guide for a client via http.Response or a server using http.ResponseWriter.</p>
<p>Feedback appreciated.
<a href="https://godoc.org/github.com/jasonrichardsmith/googlejson">https://godoc.org/github.com/jasonrichardsmith/googlejson</a>
<a href="https://github.com/jasonrichardsmith/googlejson">https://github.com/jasonrichardsmith/googlejson</a></p>
<p>Style Guide
<a href="https://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml">https://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml</a></p>
<hr/>**评论:**<br/><br/>tgaz: <pre><p>Good-looking code. The spec seems somewhat crazy, but I've never seen it before, so it might just be initial chock.</p>
<p>Feedback:</p>
<ul>
<li>Since you have a type called <code>Response</code>, naming the function <code>NewFromHTTPResponse</code> would be more precise (otherwise it can be confused with a clone function). Same with <code>WriteToResponse</code>.</li>
<li>It <em>may</em> be prettier to let <code>Write</code> fail if the item count is not consistent rather than mutating state. It's documented behavior, so it's just a matter of taste for immutability.</li>
<li><code>Write</code> discards the error returned by <code>ResponseWriter.Write</code>.</li>
<li>Rename <code>Response.Write</code> to <code>JSONBytes</code>, or <code>Marshal</code>. (Not <code>MarshalJSON</code> since that would make it a recursive <code>json.Marshaler</code>...)</li>
<li>Remove the item iteration from <code>Data</code>. Make it a separate cursor object or global functions (that might just return a slice copy instead). Having it in <code>Data</code> adds no encapsulation benefits since <code>Items</code> is already public and <code>item</code> is never transmitted anyway.</li>
<li>Replace <code>AddFields</code> with a variadic <code>AddField</code>, like we are used to with <code>append</code>. Could extend <code>AddItem</code> the same way.</li>
<li>Why is <code>SetItemCount</code> a public function? You already have mutators for the items. Make the API orthogonal/minimal.</li>
<li><code>make([]json.RawMessage, 0)</code> is the same as <code>nil</code> in this context. This also means <code>NewError</code> just returns the zero-value for <code>Error</code> (and same for other <code>New*</code> functions), so is not really adding anything. (<a href="http://play.golang.org/p/DyQ65p12RE">http://play.golang.org/p/DyQ65p12RE</a>)
<ul>
<li>Unlike maps, where <code>nil</code> is not the same as the empty map. <em>sigh</em>.</li>
</ul></li>
</ul></pre>jasonrichardsmith: <pre><p>Thank you, this was very constructive and helpful feedback. You don't always see that and it is appreciated.</p></pre>tgaz: <pre><p>NP. You don't always see well-written code with tests and documentation posted on Reddit, so thanks for sharing. :)</p></pre>jasonrichardsmith: <pre><blockquote>
<p>Replace AddFields with a variadic AddField, like we are used to with append. Could extend AddItem the same way.</p>
</blockquote>
<p>I am pretty sure you can use variadic for interface{}</p></pre>lordmatrix: <pre><p>Is there a reason for choosing GPL over a more liberal license like MIT/BSD?</p></pre>jasonrichardsmith: <pre><p>I prefer the GPL over BSD. That being said I am going to move this package to LGPL, since it is really a library that does nothing on its own.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传