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条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装