TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Go Things I Love: Channels and Goroutines

192 pointsby iamjfuover 5 years ago

10 comments

cleover 5 years ago
The example under &quot;Communicating by sharing memory&quot; isn&#x27;t correct, despite the author claiming that &quot;it works&quot;. It&#x27;s a very common example in concurrency 101 (updating a value). The fact that the author claims that it&#x27;s correct is pretty concerning to me.<p>Adding a print(len(ints)) at the bottom of the function:<p><pre><code> $ go run test.go 5 $ go run test.go 8 </code></pre> More on-topic, channels have their own tradeoffs. I often reach for WaitGroups and mutexes instead of channels, because things can get complicated fast when you&#x27;re routing data around with channels...more complicated than sharing memory. I don&#x27;t think it&#x27;s good advice to broadly recommend one over the other--understand their tradeoffs and use the right tool for the job at hand.
评论 #21977661 未加载
评论 #21977938 未加载
评论 #21979571 未加载
评论 #21980831 未加载
评论 #21981825 未加载
评论 #21978727 未加载
评论 #21977513 未加载
rubyn00bieover 5 years ago
I guess I wish this was a bit more in-depth as to what &quot;go&quot; can do with channels or goroutines, but maybe I&#x27;ve just been using languages where all this is already possible. The article is a nice cursory glance, I just want to learn more :)<p>I mean the first example it looks like is using a Mutex, and then (b)locking on it, and the second just looks like having a queue of messages (mailbox) that it rips through (like the actor pattern).<p>Some questions I&#x27;ve got after reading this...<p>How does the go-runtime (?) schedule these calls? Does it manage an internal thread pool? Is the scheduler asynchronous, parallel, or both? How do you manage contention or back pressure if I begin to flood messages to to one channel (or many)? How many channels can I have open and what are the limits? Can I still lock the system stupidly using channels, if so, how (or how not)?<p>Edit: Truly, I&#x27;m curious because as I researched asynchronous programming and efforts to better future proof my career (years ago) as we began really increasing core counts-- Go never stood out. It&#x27;s a fairly practical language, yes, but if I want a better paradigm for asynchronous programming the future it really isn&#x27;t there (IMHO). BEAM stood out as something unique, the JVM stood out as something even more practical, and Rust stood out as something performant (with the serious caveat of not being C or C++), while Go has always seemed like an odd one to me... People talk about channels and goroutines like their special but they seem pretty damn run of the mill to me... WAT AM I MISSING?
评论 #21977340 未加载
评论 #21977314 未加载
评论 #21978054 未加载
评论 #21978238 未加载
评论 #21977297 未加载
mathwover 5 years ago
I do like that select statement which hits the first case that has its channel ready with a message. That&#x27;s very nice. And having channels in your standard library is brilliant and everyone should do it.<p>A shame about the shared memory thing though. I firmly believe that designing a language where memory is shared by default is a Bad Idea. You should probably provide a way to allow it when you really need it (for performance, usually, in very very very carefully-designed code), but having memory sharing by default is a source of soooooo many bugs.<p>I know, because I&#x27;ve caused most of them.
meddlepalover 5 years ago
I&#x27;ve found channels create more complexity than their usually worth and it&#x27;s often simpler, more readable, and more maintainable to just use a sync.Mutex or sync.RWMutex.
评论 #21982149 未加载
pojntfxover 5 years ago
Channels are really nice; I love writing &quot;workers&quot; with them and sending errors and &quot;status messages&quot; with a simple `status &lt;- &quot;starting supernode&quot;`&#x2F;`errors &lt;- err`; doing this with i.e. Node&#x27;s async&#x2F;await is just so much more complex.
_pmf_over 5 years ago
Go channels are nicest concurrency mechanism I know. They bring nice ergonomics to the conceptual simplicity of a select()&#x2F;epoll() loop.
coder006over 5 years ago
Apart from the main topic, really liked the layout and theming of your blog. Curious to know of it&#x27;s hosted somewhere or self built.
评论 #21979865 未加载
osrecover 5 years ago
For anyone interested in using channels and coroutines in PHP, swoole (<a href="https:&#x2F;&#x2F;github.com&#x2F;swoole&#x2F;swoole-src" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;swoole&#x2F;swoole-src</a>) provides a reasonable implementation!
dickeytkover 5 years ago
off tangent, but I really like that syntax of defining types especially for channels:<p><pre><code> type Foo(chan&lt;- int) </code></pre> instead of what I usually see<p><pre><code> type Foo chan&lt;- int </code></pre> unfortunately it doesn&#x27;t appear compatible with gofmt (entirely), which changes it to:<p><pre><code> type Foo (chan&lt;- int) </code></pre> I still think it&#x27;s a good pattern for channels though. It makes it a lot clearer what the type is especially if you have a slice of channels:<p><pre><code> type Foo []chan&lt;- int </code></pre> vs<p><pre><code> type Foo [](chan&lt;- int)</code></pre>
aptaover 5 years ago
The only thing golang has going for it is &quot;goroutines&quot;. Now that all other popular languages are getting some variant of async (e.g. C#) or green thread implementations (e.g. Java), it will be tough to advocate for golang for new projects given its severe shortcomings.
评论 #21982554 未加载
评论 #21982312 未加载
评论 #21980935 未加载
评论 #21980256 未加载