I am new to Go and also looked abit on the thread "throw: all goroutines are asleep", but I am still wondering why this piece of code deadlock. I believe that I put a number in namesInDir, and should be able to print it afterwards. It seems that I can not add the number to the channel - which confuses me. Anyone that can help me?
type uniprot struct
{
namesInDir chan int
}
func main(){
u := uniprot{}
u.namesInDir = make(chan int)
u.namesInDir <- 1
//u.readFilenames(os.Args[1])
u.printName()
}
func (u* uniprot) printName(){
name := <-u.namesInDir
fmt.Println(name)
}
I got some suggestion and that I could cheat by buffering the channel. Why is not this working?
u.namesInDir = make(chan int, 100)
u.namesInDir <- 1
for i := 0; i < 10; i++ {
go u.printName()
}