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