美文网首页惊喜程序问题集
惊喜程序问题集(19) - 神奇的合唱

惊喜程序问题集(19) - 神奇的合唱

作者: cs_cl | 来源:发表于2017-09-25 16:39 被阅读0次
  1. 运行下面这段代码,并解释它的输出
  2. 将main函数中go sing(chordC)这一行的chordC改为mute后再次运行,并解释它的输出
//chorus.go

package main

import (
    "fmt"
    "runtime"
)

func mute() {
    for i := 0; i < 1000000000; i++ {
    }
}

func sing(tones func()) {
    for {
        mute()
        tones()
    }
}

func chordC() {
    fmt.Println("Do Mi So")
}

func chordG() {
    fmt.Println("So Ti Re")
}

func main() {
    n := runtime.NumCPU()
    for i := 0; i < n; i++ {
        go sing(chordC)
        //go sing(mute)
    }
    sing(chordG)
}

相关文章

网友评论

    本文标题:惊喜程序问题集(19) - 神奇的合唱

    本文链接:https://www.haomeiwen.com/subject/ocjdextx.html