go-learning/5concurrency/channels.go

25 lines
715 B
Go
Raw Permalink Normal View History

2023-08-26 10:51:33 +03:00
package main
import (
"fmt"
)
func hello(done chan int) {
fmt.Println("goroutine worked")
done <- 101101
}
func main() {
fmt.Println("Channels are a typed conduit through which you can send and receive values with the channel operator, <-")
myChannel1 := make(chan int)
fmt.Println("When data is sent to a channel, the control is blocked in the send statement until some other Goroutine reads from that channel")
fmt.Println("Similarly, when data is read from a channel, the read is blocked until some Goroutine writes data to that channel")
go hello(myChannel1)
fmt.Println(<-myChannel1)
fmt.Println("Calling channels causes main function wait to terminate")
fmt.Println("end of main function")
}