英文:
Go: fatal error: all goroutines are asleep - deadlock
问题
我有一个文本文件,里面只有一行文字。我想把这些单词分别存储在一个通道中,然后从通道中提取它们并逐个打印出来。我有以下代码:
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)
}
}
但是当我运行它时,我得到以下错误:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]: main.main()
我尝试在网上查找,但仍然无法解决它。请问有人可以告诉我为什么会发生这种情况,以及如何解决它吗?
谢谢!
英文:
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
得分: 7
你的file1chan
是无缓冲的,所以当你尝试向该通道发送一个值时,它会永远阻塞,等待有人获取一个值。
你需要启动一个新的goroutine,或者将通道设置为带缓冲的并将其用作数组。这是使用另一个goroutine的版本:
func main() {
f, _ := os.Open("D:\\input1.txt")
scanner := bufio.NewScanner(f)
file1chan := make(chan string)
go func() { // 启动一个新的goroutine,将字符串发送到file1chan
for scanner.Scan() {
line := scanner.Text()
// 在空格上分割行
parts := strings.Fields(line)
for i := range parts {
file1chan <- parts[i]
}
}
close(file1chan)
}()
print(file1chan) // 从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) // 缓冲区大小为1
for scanner.Scan() {
line := scanner.Text()
// 在空格上分割行
parts := strings.Fields(line)
for i := range parts {
file1chan <- parts[i]
}
}
close(file1chan) // 我们现在发送到该通道的操作已完成,所以关闭它。
print(file1chan)
}
func print(in <-chan string) {
for str := range in { // 读取直到通道关闭的所有值
fmt.Printf("%s\n", str)
}
}
英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论