Channels are Good! Channels are Bad! Goroutines are Good! Goroutines are Bad! Mutex is Good! Mutex is Bad!

blov · · 498 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I think I&#39;m getting utterly confused. There is so much polarised dialogue around good and bad patterns with Go I feel like I&#39;ve got absolutely no idea how to do anything....</p> <p>Are there good open source examples of APIs, websites etc. that are high performing, with clear use of good design patterns etc with Go, so that I can reset my brain somewhat?</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p>Don&#39;t worry about it. Do what you think makes sense and let good patterns diffuse into your brain over time. Participating in projects with presubmit codereviews helps (for example go itself and the golang.org/x repos). Most of the discussion is quibbling on a very high level, i.e. no matter what you do in those cases, you will probably end up with something decent (and I say that as someone who participates in that quibbling <em>big time</em>).</p></pre>adam_0: <pre><p>There&#39;s also <a href="http://codereview.stackexchange.com/">the code review stack exchange</a> I just discovered the other day, if you want people to critique your technique.</p></pre>izuriel: <pre><p>I wonder how long before people start equating this one (with the same rules and functionality) to the unfairness and horrid environment that StackOverflow supposedly has.</p> <p>Also, thanks for the share that looks really interesting.</p></pre>Ainar-G: <pre><p>(Just my opinion.)</p> <p>Saying &#34;mutex is bad&#34; and &#34;channels are good&#34; is like saying &#34;strings are bad&#34; and &#34;structures are good&#34;. They can&#39;t be good or bad, they can only be good or bad <em>for a particular task at hand.</em> If you need a mutex, you shouldn&#39;t try and shoehorn a channel-based solution. And vice versa, when a problem requires concurrent messaging, creating your own solution by building pseudo-channels with mutexes, conds, and slices is just silly.</p> <p>As for OP&#39;s questions, the Go concurrency patterns slides have nice examples of how and when to use channels:</p> <ul> <li><p><a href="http://talks.golang.org/2012/concurrency.slide">http://talks.golang.org/2012/concurrency.slide</a></p></li> <li><p><a href="http://talks.golang.org/2013/advconc.slide">http://talks.golang.org/2013/advconc.slide</a></p></li> </ul></pre>LemurFromTheId: <pre><p>&#34;There are only two kinds of languages: the ones people complain about and the ones nobody uses.&#34; – Bjarne Stroustrup</p></pre>lolomfgkthxbai: <pre><p>Quite so!</p></pre>dinkumator: <pre><p>Generalizations are bad! Except when they&#39;re good!</p> <p>It&#39;s all about the right tool for the job. If you need huge throughput numbers you shouldn&#39;t rely on blanket statements, you should learn about the idiosyncrasies and tradeoffs of each technique.</p> <p>Generalized statements are good when they help you understand complex systems at a high level, but bad when they are applied like a set of rules to every piece of software.</p></pre>TheMerovius: <pre><blockquote> <p>Generalizations are bad! Except when they&#39;re good!</p> </blockquote> <p>I always say &#34;all generalizations are wrong, including this one&#34; :)</p></pre>bonekeeper: <pre><p>Benchmark. Repeat.</p></pre>random314: <pre><p>There are good and bad use cases for virtually everything in this world. Go channels and routines included.</p></pre>izuriel: <pre><p>First off, any article titled &#34;X is bad&#34; (or &#34;X is good&#34;) need to be taken with a grain of salt. You&#39;re immediately given the information that you&#39;re about to read someone&#39;s opinion and the outcome they&#39;re complaining (or parading) may be that of an accident or misunderstanding. I&#39;ve been guilty of barring something from my tool belt because I thought it was highly error prone only later to find out I was using it the wrong way and when used properly there was absolutely nothing <em>inherently wrong</em> with it. What does this mean? Read the article, really put some thought into the complains/praise and do your diligence. Blog posts and articles now-a-days are so easy to create you&#39;ve got people from beginners (not that there is anything wrong with this) to experts writing posts about X and Y left and write, day after day that you have to be extra diligent about the accuracy of what you&#39;re reading.</p> <p>I think it&#39;s great that we have such easy access to vast arrays of information because some people have ideas about how to use and/or do things I never would have thought. Other times I&#39;ve seen reputable individuals who people implicitly openly speak against certain standard design patterns when the real meat of the article equated to &#34;uneducated engineers don&#39;t use the pattern right, therefore the pattern is bad&#34; which is so obviously false. A pattern is bad when there are no applications of it that improve performance, workflow, understand or any other common aspect of code and programs. The <a href="https://en.wikipedia.org/wiki/Bogosort" rel="nofollow">Bogosort</a> is a terrible algorithm for sorting lists of things. That is a provably bad pattern. And no point would it be useful or wise to use this algorithm to sort a data set.</p> <p>Anyways, I ramble. <strong>tl;dr</strong> people are prone to ranting about things they misunderstand or dislike (or praise things they misunderstand or absolutely found extremely useful) but that, in itself, doesn&#39;t make those opinions the stone cold fact. Read and research what you learn, add to your what you need and understand and be careful when someone speaks in absolutes. Absolute truths are an impossibility.</p></pre>petulant_snowflake: <pre><p>Like everything in programming, there are right and wrong applications of any technology. It takes experience, wisdom, and a hundred thousand times doing it the wrogng way before you are able to identify and understand when something like a channel or goroutines are the right thing for the job. Even the best out there still make mistakes and will often go back and revise.</p> <p>Knowing who to listen to also takes a lot of experience and knowledge in its own right, and is complicated by the fact that a lot of devs out there who are in the 60th percentile think they are in the 99th percentile and often post lame articles expressing outrage or disgust about tech XYZ. For instance the blog article that made the rounds yesterday about how channels and goroutines sucked. I read half way through the long winded post before I came to the conclusion the author knew basically nothing about properly building concurrent apps in Go and <strong>when</strong> to use that design/language feature.</p> <p>Knowing kung fu isn&#39;t as important as knowing <strong>when</strong> to use kung fu.</p></pre>robertmeta: <pre><blockquote> <p>There is so much polarized dialogue around good and bad patterns with Go I feel like I&#39;ve got absolutely no idea how to do anything...</p> </blockquote> <p>Stop looking for silver bullets (&#34;design patterns&#34;, &#34;the one true way&#34;). You have to be flexible and adapt your approach to your problem. Callbacks work for some problems, but you end up in callback hell on other problems. Channels are amazing for some problems (ETL) and worse for others. I actually haven&#39;t seen anyone really so goroutines or mutexes are bad... but, they aren&#39;t. </p> <blockquote> <p>Are there good open source examples of APIs</p> </blockquote> <p>The go standard library is your &#34;go&#34; to for examples of how to write go. </p></pre>mcouturier: <pre><p>My 2 cents here...</p> <p>Whenever I read an article about something bad in Go, there&#39;s almost always a clear explanation why it is the way it is on the <a href="https://golang.org/doc/" rel="nofollow">official language website</a>.</p> <p>I say, stick to the core documentation and its close surroundings like <a href="https://gobyexample.com" rel="nofollow">gobyexample.com</a> and follow their examples. Don&#39;t hesitate to get code reviews on the Go <a href="https://groups.google.com/forum/#!forum/golang-nuts" rel="nofollow">mailing list</a>. People there are awesome and always willing to help. Use the <a href="https://play.golang.org/" rel="nofollow">playground</a> to demonstrate to others what you are trying to do to get code reviews.</p> <p>Go is awesome but also very opinionated. So that&#39;s normal that there are some polarized discussions around it. Take the good things, build on it.</p></pre>itsmontoya: <pre><p>Channels aren&#39;t as performant as they should be. That being said, they aid in constructing VERY idiomatic code which is still fast &#34;enough&#34;. </p> <p>For services where clean code is important and being fast enough is acceptable, we utilize channels a decent amount. In the situations where we need more speed than that, we use mutex&#39;s. Additionally, when we need even MORE speed, we work directly with the atomic library. </p></pre>mostlywaiting: <pre><p>I&#39;d say that channels are great when you are using them to handle resources that may have significant latency. Then their overhead is not significant and the ease of use is a huge win.</p></pre>itsmontoya: <pre><p>Yes, this would be an example of a useful situation for channels</p></pre>sybrandy: <pre><p>I like this answer. I like reading the articles that do go into why X is bad, but in many cases the built-ins are good enough. I actually want to go back to some code I wrote to make it faster, but not because I need to because it&#39;s performed very well with channels. I just want to make it better.</p> <p>Just like any other critical article, take it with a grain of salt. There&#39;s a chance it has some good info that can be useful. Just don&#39;t believe that all that is bad is really, truly bad.</p></pre>gruey: <pre><p>For statements are good! For statements are bad!</p></pre>f2f: <pre><p>learn rust. there&#39;s nothing bad there. until it all changes, and then the new thing is good and the old is not talked about in polite company :)</p></pre>jessegreathouse: <pre><p>I agree with half of those complaints!</p></pre>jasonrichardsmith: <pre><p>Seconded!</p></pre>

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

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