A channel in Go is a communication mechanism that allows goroutines to communicate and synchronize with each other safely. Think of it as a "pipeline" or "conveyor belt" that passes data between goroutines. It is blocking operation - that means if you send 1 to a channel, you are expected to read from it too.
To declare a channel we use make.
Channel are use to send and receive data. To say our function can send data or write to a channel, we
func sendOnly(ch chan<- int) {
fmt.Println("Sending 42...")
ch <- 42
fmt.Println("Sent 42")
}
To get data from a channel we write as follows
func receiveOnly(ch <-chan int) {
value := <-ch
fmt.Println("Received:", value)
}
For example you have the following code
package main
import "fmt"
func receiveOnly(ch <-chan int) {
value := <-ch
fmt.Println("Received:", value)
}
func sendOnly(ch chan<- int) {
fmt.Println("Sending 42...")
ch <- 42
fmt.Println("Sent 42")
}
func main() {
ch := make(chan int, 1)
go sendOnly(ch)
receiveOnly(ch) // This blocks until it receives a value
fmt.Println("Main function continuing...")
}
What happens if you send but don't received?
func main() {
ch := make(chan int, 1)
go sendOnly(ch)
//receiveOnly(ch) // This blocks until it receives a value
fmt.Println("Main function continuing...")
}
program would just continue to run and then exit - outputing
Main function continuing...
What happens if you received but didn't send anything to the channel?
func main() {
ch := make(chan int, 1)
//go sendOnly(ch)
receiveOnly(ch) // This blocks until it receives a value
fmt.Println("Main function continuing...")
}
You will get panic as follows.
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.receiveOnly(0x0?)
/tmp/sandbox4167387200/prog.go:6 +0x25
main.main()
/tmp/sandbox4167387200/prog.go:20 +0x25
Program exited.
Since our channel has a buffer capacity of 1, what if you tries to send 2 or 3 value to it?
package main
import "fmt"
func receiveOnly(ch <-chan int) {
value := <-ch
fmt.Println("Received:", value)
}
func sendOnly(ch chan<- int) {
fmt.Println("Sending 42...")
ch <- 42
ch <- 43
ch <- 44
fmt.Println("Sent 42")
}
func main() {
ch := make(chan int, 1)
go sendOnly(ch)
receiveOnly(ch) // This blocks until it receives a value
fmt.Println("Main function continuing...")
}
Since your program receive value from a channel once, then you would receive one value. No panic
Sending 42...
Received: 42
Main function continuing...
Program exited
Just for clarifiy, even though we declare our channel with a buffer 1, although we send in 3 data value to it, 41, 42 and 43 we would still be able to get the value back.
package main
import "fmt"
func receiveOnly(ch <-chan int) {
value := <-ch
fmt.Println("Received:", value)
}
func sendOnly(ch chan<- int) {
fmt.Println("Sending 42...")
ch <- 42
ch <- 43
ch <- 44
fmt.Println("Sent 42")
}
func main() {
ch := make(chan int, 1)
go sendOnly(ch)
receiveOnly(ch) // This blocks until it receives a value
receiveOnly(ch)
receiveOnly(ch)
fmt.Println("Main function continuing...")
}
So the code would ouputs
Sending 42...
Received: 42
Received: 43
Received: 44
Main function continuing...
Comments