download92000 2015-03-10 07:28
浏览 167
已采纳

Go中的goroutine没有输出

While SayHello() executes as expected, the goroutine prints nothing.

package main

import "fmt"

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

func main() {
    SayHello()
    go SayHello()
}
  • 写回答

3条回答 默认 最新

  • douxian7117 2015-03-10 07:32
    关注

    When your main() function ends, your program ends as well. It does not wait for other goroutines to finish.

    Quoting from the Go Language Specification: Program Execution:

    Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.

    See this answer for more details.

    You have to tell your main() function to wait for the SayHello() function started as a goroutine to complete. You can synchronize them with channels for example:

    func SayHello(done chan int) {
        for i := 0; i < 10; i++ {
            fmt.Print(i, " ")
        }
        if done != nil {
            done <- 0 // Signal that we're done
        }
    }
    
    func main() {
        SayHello(nil) // Passing nil: we don't want notification here
        done := make(chan int)
        go SayHello(done)
        <-done // Wait until done signal arrives
    }
    

    Another alternative is to signal the completion by closing the channel:

    func SayHello(done chan struct{}) {
        for i := 0; i < 10; i++ {
            fmt.Print(i, " ")
        }
        if done != nil {
            close(done) // Signal that we're done
        }
    }
    
    func main() {
        SayHello(nil) // Passing nil: we don't want notification here
        done := make(chan struct{})
        go SayHello(done)
        <-done // A receive from a closed channel returns the zero value immediately
    }
    

    Notes:

    According to your edits/comments: if you want the 2 running SayHello() functions to print "mixed" numbers randomly: you have no guarantee to observe such behaviour. Again, see the aforementioned answer for more details. The Go Memory Model only guarantees that certain events happen before other events, you have no guarantee how 2 concurrent goroutines are executed.

    You might experiment with it, but know that the result will not be deterministic. First you have to enable multiple active goroutines to be executed with:

    runtime.GOMAXPROCS(2)
    

    And second you have to first start SayHello() as a goroutine because your current code first executes SayHello() in the main goroutine and only once it finished starts the other one:

    runtime.GOMAXPROCS(2)
    done := make(chan struct{})
    go SayHello(done) // FIRST START goroutine
    SayHello(nil) // And then call SayHello() in the main goroutine
    <-done // Wait for completion
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应