for example:
type name struct {
name string
age int
}
func main() {
c := make(chan name)
c <- name{"sfsaf", 1}
a, b := <- c
close(c)
}
The result:
fatal error: all goroutines are asleep - deadlock!
I want to pass values through channel. What should I do?
Yes you can pass structs. But that's not the problem in your OP.
You sent a value on a channel, when there was no receiver ready to receive. That is what caused your deadlock.
Channels expect the receiver
to be blocking, waiting for the sender
. This is done with Goroutines.
Therefore, wrap your sender in a goroutine, which will not execute right away.
package main
import (
"fmt"
)
type name struct {
name string
age int
}
func main() {
c := make(chan name)
go func() {
c <- name{"sfsaf", 1}
close(c)
}()
for n := range c {
fmt.Println(n)
}
fmt.Println("channel was closed (all done!).")
}
See it in the playground: https://play.golang.org/p/uaSuCaB4Ms
This works because the sender's goroutine is not executing yet. Not until the current goroutine executing gets blocked.
And we get blocked on the for n := range c
loop. This is the receiver, sitting and waiting for values. (It is a common pattern to use the for loop to iterate over channel values as it will sit and block, waiting for values).
So now that we are blocked waiting to receive values in the for
loop, the inline gorouting will now execute, to send our value on the channel.
In addition, we follow safe practices and tidy up after ourselves and close(c)
the channel, signalling the for
loop or select
statement that there will be no more values sent. The sender always closes, never the receiver. This is the pattern the for
range loop uses to exit the for loop, and continue executing the rest of your code.
As a side note, you did well by passing the value of the struct - not a pointer.
If you passed a pointer, you'd have to implement some mutex lock around the object to prevent a R/W panic.
Do not communicate by sharing memory; instead, share memory by communicating.
Stick to passing values, not pointers, around your channels and goroutines and reap the benefits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With