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

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含