stian
stian

Reputation: 1987

why are all goroutines deadlocked?

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()
}

Upvotes: 0

Views: 189

Answers (1)

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54117

Buffering the channel works like this.

A channel with no buffer blocks the sender until the receiver takes the value. In your original example you only have one go routine so all go routines are blocked when you send the integer. A buffer overcomes this. Alternatively run two go routines - one sending and one receiving.

package main

import "fmt"

type uniprot struct {
    namesInDir chan int
}

func (u *uniprot) printName() {
    name := <-u.namesInDir
    fmt.Println(name)
}

func main() {
    u := uniprot{}
    u.namesInDir = make(chan int, 1) // Buffer added here
    u.namesInDir <- 1
    //u.readFilenames(os.Args[1])
    u.printName()
}

Upvotes: 3

Related Questions