I am absolute newbie in Golang. I am learning through Tour of Go and then implementing ideas with my own understanding. I am having problem with goroutines. I made a unbuffered channel , then sent a string to that channel.
func main() {
p := make(chan string)
p <- "Hello goroutine"
fmt.Println(<-p)
}
throws error
fatal error: all goroutines are asleep - deadlock!
I get it, channel is unbuffered. (That's the reason. Right?).
But when I refactor p <- "Hello goroutine
to a goroutine
func main() {
p := make(chan string)
go sendHello(p)
fmt.Println(<-p)
}
func sendHello(p chan string) {
p <- "Hello goroutine"
}
It works without problem. I read that we do not need to use pointers with maps,slices and channels with most cases to modify the value. Was channel p
passed to func sendHello(p chan string)
via a copy which had a separate buffer. I still can not get my head around it.