到目前为止,我见过的所有例子都涉及阻塞来获取结果(通过<-chan运算符)。
我目前的做法涉及将指针传递给结构:
type goresult struct { result resultType; finished bool; }该门厅在完成时写入。 然后,只要方便,就可以finished检查。 你有更好的选择吗?
我真正想要的是一个Qt风格的信号插槽系统。 我有预感,解决方案看起来几乎微不足道(有很多未知的潜力),但我还不熟悉这门语言。
All the examples I've seen so far involve blocking to get the result (via the <-chan operator).
My current approach involves passing a pointer to a struct:
type goresult struct { result resultType; finished bool; }which the goroutine writes upon completion. Then it's a simple matter of checking finished whenever convenient. Do you have better alternatives?
What I'm really aiming for is a Qt-style signal-slot system. I have a hunch the solution will look almost trivial (chans have lots of unexplored potential), but I'm not yet familiar enough with the language to figure it out.
最满意答案
你可以使用“逗号,好的”模式(参见他们的“ 有效的去 ”页面):
foo := <- ch; // This blocks. foo, ok := <- ch; // This returns immediately.You can use the "comma, ok" pattern (see their page on "effective go"):
foo := <- ch; // This blocks. foo, ok := <- ch; // This returns immediately.更多推荐
发布评论