1
votes

I have a text file with just one line of words in it. I want to store all of those words separately in a channel, and then extract them all from the channel and print them one by one. I have the following code:

func main() {
    f, _ := os.Open("D:\\input1.txt")
    scanner := bufio.NewScanner(f)
    file1chan := make(chan string)   
    for scanner.Scan() {
        line := scanner.Text()

        // Split the line on a space
        parts := strings.Fields(line)

        for i := range parts {   
            file1chan <- parts[i]
        }    
    }
    print(file1chan)
}

func print(in <-chan string) {
    for str := range in {
        fmt.Printf("%s\n", str)
    }
}

But when i run it i get the following error:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]: main.main()

I tried looking it up online but i still couldn't manage to fix it. Could anybody please tell me why this is happening and how i can go about fixing it?

Thanks!

1

1 Answers

6
votes

Your file1chan is unbuffered, so when you try to send a value down that channel, it blocks forever, waiting for someone to fetch a value.

You need to start a new goroutine, or make the channel buffered and use it as an array. Here's the version with another goroutine:

func main() {
    f, _ := os.Open("D:\\input1.txt")
    scanner := bufio.NewScanner(f)
    file1chan := make(chan string)
    go func() { // start a new goroutine that sends strings down file1chan 
        for scanner.Scan() {
            line := scanner.Text()

            // Split the line on a space
            parts := strings.Fields(line)

            for i := range parts {
                file1chan <- parts[i]
            }
        }
        close(file1chan)
    }()
    print(file1chan) // read strings from file1chan
}

func print(in <-chan string) {
    for str := range in {
        fmt.Printf("%s\n", str)
    }
}

And here's the buffered version, for handling only a single string:

func main() {
    f, _ := os.Open("D:\\input1.txt")
    scanner := bufio.NewScanner(f)
    file1chan := make(chan string, 1) // buffer size of one
    for scanner.Scan() {
        line := scanner.Text()

        // Split the line on a space
        parts := strings.Fields(line)

        for i := range parts {
            file1chan <- parts[i]
        }
    }
    close(file1chan) // we're done sending to this channel now, so we close it.
    print(file1chan)
}

func print(in <-chan string) {
    for str := range in { // read all values until channel gets closed
        fmt.Printf("%s\n", str)
    }
}