doudiecai1572 2012-11-07 02:50
浏览 81
已采纳

相同的Golang代码不同的输出,为什么?

I'm trying to execute an example from golang.org : http://tour.golang.org/#63

  • I Have Changed The Code To Test What Exactly Gosched does.*

You could see the output there is: enter image description here

hello
hello
hello
hello
hello

But when i copy those code to my Mac OS X 10.8(Go version 1.0.3), the output has changed: enter image description here xxxxxx$ go version go version go1.0.3 xxxxxx$ go run goroutine.go hello world hello world hello world hello world hello world

According to this answer, i should use runtime.GoSched, but in fact i needn't.So i believe something goes wrong.

Please help me with this, many thanks.

  • 写回答

3条回答 默认 最新

  • duan89197 2012-11-07 03:24
    关注

    The issue here is you have two different implementations.

    Locally, your code yields to the scheduler each time fmt.Println is called. Println does a write to stdout which does a syscall. All syscalls yield the same way runtime.Gosched does.

    play.golang.org is a black box. We don't actually know how it works. However, from the example you gave, it appears play does not do a syscall when fmt.Println is called. This makes sense. They probably replaced os.Stdout with a buffer to hold what is printed.

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

报告相同问题?