I made two attempts to pass the channel of the function as a parameter, but both of them do not work (deadlock):
Attempt 1:
func done(signal *chan bool) { *signal <- true } func main() { signal := make(chan bool) done(&signal) <-signal fmt.Println("completed") }
Attempt 2:
func done(signal chan bool) { signal <- true } func main() { signal := make(chan bool) done(signal) <-signal fmt.Println("completed") }
Well, I have no ideas. What should be the correct way to pass a channel to a function?
done(&signal)called synchronously. Maybe you would like to call it asynchronously?
done(&signal)
place the keyword gobefore calling the function
go
go done(&signal)
The main stream will be blocked until the recorded function is recorded in the channel. And the done method will be blocked when writing to the channel until the main thread reads the channel.
, , , - . , , , , signal <- true, , .
signal <- true
goroutine, , main() , -, :
go done(signal)
, , :
signal := make(chan bool, 1)