drctyr2869 2014-08-21 03:51
浏览 36
已采纳

go例程中的执行顺序

I recently started with go and I am really confused about the order of execution of this program. I hope I am not asking really trivial questions here.

This is basically #69 in the golang tour, with some Println I have inserted; Link to playground: http://play.golang.org/p/PXDlD3EA2f

func fibonacci(c, quit chan int) {
    x, y := 0, 1

    fmt.Println("Inside the fibonacci")

    for {
        select {
        case c <- x:
            fmt.Println("Inside the for, first case, before reassigning ", x, y)
            x, y = y, x+y
            fmt.Println("Inside the for, first case, after reassigning ", x, y)
        case <-quit:
            fmt.Println("quit")
            return
        }
    }
}

func main() {
    fmt.Println("Begin of Main")
    c := make(chan int)
    quit := make(chan int)
    fmt.Println("Before gonig to the func")
    go func() {
        fmt.Println("Inside go routine")
        fmt.Println("Inside go routine... again")
        for i := 0; i < 10; i++ {
            fmt.Println("Inside go routine and the for, before printing the channel")
            fmt.Println(<-c)
        }
        quit <- 0
    }()
    fmt.Println("Before calling to fibonacci")
    fibonacci(c, quit)
    fmt.Println("Closing")
}

In the very verbose output I obtain (see attached image below), there are a couple of things I do not understand:

  • why is the "Inside the fibonacci" line before the ones in the go routine? Is this because after the go command, the compiler is just reading at the same time inside func and fibonacci?

  • how do fibonacci and func interact? func is not changing the channel c, so why is fibonacci reacting? Who's changing c?

  • why inside fibonacci there are 5 prints together every time? Honesty I was expecting only 2.

Output of the function:

enter image description here

  • 写回答

2条回答 默认 最新

  • dongxichan8627 2014-08-21 04:11
    关注

    Let's take it step by step:

    * why is the "Inside the fibonacci" line before the ones in the go routine? Is this because after the go command, the compiler is just reading at the same time inside func and fibonacci?

    Because your go routine actually starts after you call fibonacci, the scheduler takes a little bit of time to start, for example if you start a goroutine that calls fmt.Println and do nothing to wait in main, the program will exit before it gets executed.

    * how do fibonacci and func interact? func is not changing the channel c, so why is fibonacci reacting? Who's changing c?

    fibonacci is pushing numbers into the channel, notice this part:

    select {
            case c <- x: //this is sending x to the channel
    

    * why inside fibonacci there are 5 prints together every time? Honesty I was expecting only 2.

    That again depends on the scheduler and the fact that fmt.Print* doesn't lock stdout, the output can happen in any order since it's 2 different threads printing stuff.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作