$ ./pypy -O Python 2.7.2 (a3e1b12d1d01, Dec 04 2012, 13:33:26) [PyPy 1.9.1-dev0 with GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. And now for something completely different: `` amd64 and ppc are only available in enterprise version'' >>>> assert 1==2 Traceback (most recent call last): File "", line 1, in AssertionError >>>>
但是当我执行时
$ python -O Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> assert …
我们可以通过make函数创建通道,通过{}表达式创建新对象.
ch := make(chan interface{})
o := struct{}{}
Run Code Online (Sandbox Code Playgroud)
但是,有什么区别make和{}新的地图吗?
m0 := make(map[int]int)
m1 := map[int]int{}
Run Code Online (Sandbox Code Playgroud) 在Golang,当我们需要等待完成某些事情时,我们将使用一个频道等待它完成.
例:
done := make(chan struct{})
go func() {
// ...
close(done)
}()
<-done
Run Code Online (Sandbox Code Playgroud)
但是,换句话说,chan interface{}在这种情况下工作正常.
那么,chan struct{}和之间有什么区别chan interface{}?
例2:
done := make(chan struct{})
go func() {
// ...
done <- struct{}{}
}()
<- done
Run Code Online (Sandbox Code Playgroud)
在其他情况下,如果不关闭goroutine中的通道而不是发送对象.
将在goroutine中创建一个对象,但如果使用chan interface{},则可以将nil对象发送到通道.
相反,这是一个更好的方法chan struct{}吗?