相关疑难解决方法(0)

为什么这个Go代码会阻塞?

我写了以下程序:

package main

import (
    "fmt"
)

func processevents(list chan func()) {
    for {
        //a := <-list
        //a()
    }
}

func test() {
    fmt.Println("Ho!")
}

func main() {

    eventlist := make(chan func(), 100)

    go processevents(eventlist)

    for {
        eventlist <- test
        fmt.Println("Hey!")
    }
}
Run Code Online (Sandbox Code Playgroud)

由于频道事件列表是一个缓冲频道,我想我应该得到输出"嘿!"的100倍,但它只显示一次.我的错误在哪里?

go goroutine

12
推荐指数
2
解决办法
4185
查看次数

golang:goroute with select不会停止,除非我添加了fmt.Print()

我尝试了Go Tour 练习#71

如果它运行go run 71_hang.go ok,它工作正常.

但是,如果您使用go run 71_hang.go nogood,它将永远运行.

唯一的区别是额外fmt.Print("")defaultselect语句.

我不确定,但我怀疑某种无限循环和竞争条件?这是我的解决方案.

注意:Go并没有死锁 throw: all goroutines are asleep - deadlock!

package main

import (
    "fmt"
    "os"
)

type Fetcher interface {
    // Fetch returns the body of URL and
    // a slice of URLs found on that page.
    Fetch(url string) (body string, urls []string, err error)
}

func crawl(todo Todo, fetcher Fetcher,
    todoList chan Todo, done chan bool) { …
Run Code Online (Sandbox Code Playgroud)

select channel go goroutine

6
推荐指数
2
解决办法
7696
查看次数

标签 统计

go ×2

goroutine ×2

channel ×1

select ×1