Created
January 15, 2020 14:22
-
-
Save ismdeep/44b56aa0e349b3e696db515c4d486905 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "sync" | |
type receiver struct { | |
sync.WaitGroup | |
data chan int | |
} | |
func newReceiver() *receiver { | |
r := &receiver{ | |
data: make(chan int, 1000000), | |
} | |
r.Add(1) | |
go func(dataChan chan int) { | |
for x := range dataChan{ | |
println("recv : ", x) | |
} | |
defer r.Done() | |
}(r.data) | |
return r | |
} | |
func main() { | |
r := newReceiver() | |
for i := 0; i < 100000; i++ { | |
r.data <- i | |
} | |
close(r.data) | |
r.Wait() | |
println("Done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
newReceiver()
里面的,那么出来之后就认为实体没有存在的必要了。可能会被自动回收掉。