Better counter implementation with channel

blov · · 547 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have came up with the following code, but I don&#39;t like that I have to catch a panic message. Is there a better way to have counters implemented with channels?</p> <p>package lce</p> <pre><code>import ( &#34;log&#34; ) type Counter struct { next int ch chan int } func NewCounter(next_id int) *Counter { return &amp;Counter{ next: next_id, ch: make(chan int), } } func (self Counter) Run() { go func() { defer func() { if r := recover(); r != nil { log.Printf(&#34;Counter run loop is terminating. Detail: %v\n&#34;, r) } }() for { if self.ch != nil { self.ch &lt;- self.next self.next++ } else { log.Print(&#34;Counter is closed.&#34;) } } }() } func (self Counter) Close() { if self.ch != nil { close(self.ch) self.ch = nil } } </code></pre> <hr/>**评论:**<br/><br/>condanky: <pre><p>you can use mutex locks around an increment function instead of using channels. Are you using the count somewhere else?</p></pre>TheMerovius: <pre><p>In another thread, a while back, I came up with <a href="https://github.com/Merovius/go-misc/blob/master/owned/owned.go" rel="nofollow">this pattern</a> which is kind of neat. The <code>New</code> function encapsulates the logic. An increment would be expressible by passing in <code>func(v interface{}) interface{} { return v.(int) + 1 }</code>. More readable would be, to change the type used in the code from the generic <code>interface{}</code> to <code>int</code>. In that case it&#39;s</p> <pre><code>v := New(0) inc := func(i int) int { return i + 1 } v &lt;- inc v &lt;- inc </code></pre> <p>This pattern is at least much more readable and stable than yours :)</p> <p>All of that being said: Using a mutex is a lot simpler and faster. Using <a href="https://godoc.org/sync/atomic#AddInt64" rel="nofollow">sync/atomic</a> even more so (though that&#39;s probably overkill and too subtle for normal programs. Just use a mutex).</p></pre>captncraig: <pre><p>You should check out this talk from gophercon: <a href="https://www.youtube.com/watch?v=1V7eJ0jN8-E" rel="nofollow">https://www.youtube.com/watch?v=1V7eJ0jN8-E</a></p> <p>You think he&#39;s gonna talk about this big monitoring system he built, but he really spends 20 minutes talking about how to increment a number aomically. He ends up with a pretty cool usage of the <code>sync/atomic</code> stuff.</p></pre>DarkRye: <pre><p>Thank you. I appreciate the link.</p></pre>DarkRye: <pre><p>It does fee like an overkill. I am learning go as I go.</p></pre>alienwork: <pre><p>Use the atomic package for this. <a href="https://golang.org/pkg/sync/atomic/#AddInt32" rel="nofollow">https://golang.org/pkg/sync/atomic/#AddInt32</a></p></pre>gohacker: <pre><p>Wow, what a freaking misuse of channels!</p></pre>

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

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