Simple goroutine not working on Windows

前端 未结 2 990
臣服心动
臣服心动 2021-01-12 09:02

I\'m doing some tests with goroutines just to learn how they work, however it seems they are not running at all. I\'ve done a very simple test:

package main
         


        
相关标签:
2条回答
  • 2021-01-12 09:47

    program execution does not wait for the invoked function to complete

    Go statements

    Wait a while. For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func test() {
        fmt.Println("test")
    }
    
    func main() {
        go test()
        time.Sleep(10 * time.Second)
    }
    

    Output:

    test
    
    0 讨论(0)
  • 2021-01-12 09:54

    I know it's answered, but for the sake of completeness:

    Channels

    package main
    
    import (
        "fmt"
    )
    
    func test(c chan int) {
        fmt.Println("test")
    
        // We are done here.
        c <- 1
    }
    
    func main() {
        c := make(chan int)
        go test(c)
    
        // Wait for signal on channel c
        <- c
    }
    
    0 讨论(0)
提交回复
热议问题