Stringer now supports inline comments

blov · · 497 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>We can now use inline comments to generate enums with <a href="https://godoc.org/golang.org/x/tools/cmd/stringer">stringer</a>.</p> <p>The patch is <a href="https://go-review.googlesource.com/c/tools/+/44076">here</a>.</p> <p><strong>Example:</strong></p> <pre><code>//go:generate stringer -type=Token -linecomment=true package main type Token int const ( And Token = iota // &amp; Or // | Add // + Sub // - Ident Period // . ) </code></pre> <p><strong>Output</strong></p> <pre><code>package main import &#34;fmt&#34; func main() { fmt.Printf(&#34;1 %s 0&#34;, Or) } // prints: 1 | 0 </code></pre> <hr/>**评论:**<br/><br/>mvdan: <pre><p>Oh, this was me! Glad that others are finding it useful.</p> <p>If anyone has any comments about the design or implementation, you can let me know here.</p></pre>blackflicker: <pre><p>If you can write a post about &#34;how&#34; you&#39;ve implemented this, I believe that many people would find it valuable.</p></pre>mvdan: <pre><p>This particular feature was simple to implement - the logic is just two lines, as go/ast already keeps track of inline comments on value specs (declarations).</p> <p>I&#39;ve been doing other changes to stringer recently too, such as prefix trimming and fewer dependencies on generated code. Perhaps I should write on this, but I have never liked having a blog.</p></pre>blackflicker: <pre><p>Yep, simple for me too, but not for everyone. If you write about it I can publish it or just send it here, you don&#39;t need a blog to do that. As you wish :)</p></pre>0xjnml: <pre><p>Should have been IMO <code>// &#34;&amp;&#34;</code> etc. Sorry to miss that CL before committing. </p></pre>xargon7: <pre><p>That&#39;s the format I took in my fork: <a href="https://github.com/augustoroman/enumer" rel="nofollow">https://github.com/augustoroman/enumer</a></p></pre>jackie_pwn_asses: <pre><p>Go&#39;s metaprogramming model is an endless source of comedy.</p></pre>benhoyt: <pre><p>Maybe it&#39;ll spoil the joke, but can you explain what you mean in more detail?</p></pre>nemith: <pre><p>Swap // for # and it&#39;s purpose is the similar to a preprocessor.</p></pre>welle: <pre><p>I think tags (like json uses) might be better because then I could still have comments for humans next to it. Neat addition though. </p></pre>jerf: <pre><p>It might be, but in the current grammar constants don&#39;t get struct tags.</p></pre>mvdan: <pre><p>If anyone is up to review, here&#39;s another little enhancement to stringer that I have just sent: <a href="https://go-review.googlesource.com/c/tools/+/85015" rel="nofollow">https://go-review.googlesource.com/c/tools/+/85015</a></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

497 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传