I wanted to understand how does the Go-routine work here.
Question 1: Does the go-routine inherit anything from the main GO routine/func??
Question 2: Also wanted to know if I send "hi whats up" into the msg channel why is it not received by the go-routine at LINE:13????
1 package main
2
3 import "fmt"
4
5 func main() {
6 msg := make(chan string, 2) //channel (bidirectional channel) of type string
7
8 msg <- "hi whats up"
9 fmt.Println("*****I am main go routine*****")
10
11 go func() {
12 fmt.Println("*****HI ABHI *****
")
13 temp := <-msg
14 fmt.Println(temp)
15 msg <- "Hello from a goroutine!" //send a string through the channel
16 }()
17
18 fmt.Println("Main routine waiting for message")
19
20 fmt.Println(<-msg) //receive the string from the channel
21 fmt.Println(<-msg)
22 }
Getting following error:
*****I am main go routine*****
Main routine waiting for message
hi whats up
*****HI ABHI *****
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
/Users//work/go/src/github.com/Golang_play/goroutine/goroutine.go:21 +0x1e6
goroutine 5 [chan receive]:
main.main.func1(0xc420054060)
/Users/work/go/src/github.com/Golang_play/goroutine/goroutine.go:13 +0x98 created by main.main /Users/work/go/src/github.com/Golang_play/goroutine/goroutine.go:11 +0xe2 exit status 2
Also in the following program LINE 14 never prints.
5 func main() {
6 msg := make(chan string, 2) //channel (bidirectional channel) of type string
7
8 msg <- "hi whats up"
9 fmt.Println("*****I am main go routine*****")
10
11 go func() {
12 fmt.Println("*****HI ABHI *****
")
13 temp := <-msg
14 fmt.Println(temp)
15 //msg <- "Hello from a goroutine!" //send a string through the channel
16 }()
You're trying to send messages two ways on the same channel, and you don't do anything to guarantee who reads which messages. It's entirely possible for the main goroutine to receive the message it sent, before the func
goroutine attempts to read from the channel. If that happens, both goroutines get stuck waiting for messages that will never arrive.
Use two channels.
The newly started goroutine can capture variables from the outer code via a closure, but otherwise don't inherit anything.
There's a race between the channel receive on line 13 and the one on line 20. If the read on line 20 gets executed first, it'll consume the only value on the channel, and ultimately, both goroutines are blocked reading from a channel, hence the deadlock error. If you want to get a value out of the goroutine back, its better to have another channel rather than trying to sequence it all on the one.
Here's a working example:
package main
import "fmt"
func main() {
msg := make(chan string)
go func() {
temp := <-msg
fmt.Println(temp)
msg <- "Hello from a goroutine!"
}()
msg <- "hi whats up"
fmt.Println(<-msg)
}
With an unbuffered channel, message passing is synchronous, so goroutines never receive messages from themselves (because the same goroutine would have to be sending and receiving at the same time for that to happen). Note that this design requires you to start the goroutine reading from the msg
channel before writing anything into it, or you'd be stuck waiting for the reader and unable to start it.