As code snippets shown below. Sample I, closes channel inside goroutine; Sample II, closes in an anonymous goroutinue then call same logic
If these two have exactly the same effect, Does it mean chan
is passed by reference?
- Sample I
func Wakeup(wakeupChan chan<- int) {
defer close(wakeupChan)
time.Sleep(5 * time.Second)
wakeupChan <- 1
}
func main() {
wakeupChan := make(chan int)
go Wakeup(wakeupChan)
<-wakeupChan
fmt.Println("wake up")
}
- Sample II
func WakeupAnother(wakeupChan chan<- int) {
time.Sleep(5 * time.Second)
wakeupChan <- 1
}
func main() {
wakeupChanAnother := make(chan int)
go func() {
defer close(wakeupChanAnother)
WakeupAnother(wakeupChanAnother)
}()
<-wakeupChanAnother
fmt.Println("wake up")
}