相关疑难解决方法(0)

Go中没有来自goroutine的输出

虽然SayHello()按预期执行,但goroutine没有打印任何内容.

package main

import "fmt"

func SayHello() {
    for i := 0; i < 10 ; i++ {
        fmt.Print(i, " ")
    }
}

func main() {
    SayHello()
    go SayHello()
}
Run Code Online (Sandbox Code Playgroud)

concurrency go goroutine

18
推荐指数
2
解决办法
1660
查看次数

如果time.Sleep包含,Goroutine不会执行

以下代码运行完全正常:

package main

import (
    "fmt"
)

func my_func(c chan int){
    fmt.Println(<-c)
}

func main(){
    c := make(chan int)
    go my_func(c)

    c<-3
}
Run Code Online (Sandbox Code Playgroud)

playgound_1

但是,如果我改变

c<-3
Run Code Online (Sandbox Code Playgroud)

time.Sleep(time.Second)
c<-3
Run Code Online (Sandbox Code Playgroud)

playground_2

我的代码没有执行.

我的直觉是mainmy_func完成执行之前以某种方式返回,但似乎添加暂停应该没有任何效果.我完全迷失在这个简单的例子上,这里发生了什么?

concurrency channel go goroutine

9
推荐指数
1
解决办法
1679
查看次数

标签 统计

concurrency ×2

go ×2

goroutine ×2

channel ×1