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!