<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 // &
Or // |
Add // +
Sub // -
Ident
Period // .
)
</code></pre>
<p><strong>Output</strong></p>
<pre><code>package main
import "fmt"
func main() {
fmt.Printf("1 %s 0", 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 "how" you'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'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't need a blog to do that. As you wish :)</p></pre>0xjnml: <pre><p>Should have been IMO <code>// "&"</code> etc. Sorry to miss that CL before committing. </p></pre>xargon7: <pre><p>That'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's metaprogramming model is an endless source of comedy.</p></pre>benhoyt: <pre><p>Maybe it'll spoil the joke, but can you explain what you mean in more detail?</p></pre>nemith: <pre><p>Swap // for # and it'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't get struct tags.</p></pre>mvdan: <pre><p>If anyone is up to review, here'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>
![](https://static.golangjob.cn/static/img/banner.png)