我有一个文本文件,里面只有一行文字。我想将所有这些单词分别存储在一个通道中,然后从通道中提取所有这些单词并逐个打印。我有以下代码:
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)
}
}
但是当我运行它时,我收到以下错误:
致命错误:所有goroutine都睡着了 - 死锁!
goroutine 1 [chan send]:main.main()
我尝试在网上查找,但我仍无法修复它。有人可以告诉我为什么会这样,以及如何解决这个问题?
谢谢!
答案 0 :(得分:5)
您的file1chan
是无缓冲的,因此当您尝试向该通道发送值时,它会永久阻塞,等待某人获取值。
您需要启动一个新的goroutine,或者将通道缓冲并将其用作数组。这是另一个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)
}
}
这是缓冲版本,只处理一个字符串:
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)
}
}