简单的goroutine在Windows上不起作用 [英] Simple goroutine not working on Windows

查看:131
本文介绍了简单的goroutine在Windows上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用goroutine进行一些测试,只是为了了解它们的工作原理,但是看来它们根本没有运行.我做了一个非常简单的测试:

I'm doing some tests with goroutines just to learn how they work, however it seems they are not running at all. I've done a very simple test:

package main

import (
    "fmt"
)

func test() {
    fmt.Println("test")
}

func main() {
    go test()
}

我希望它可以打印测试",但是它什么也不做,没有消息,也没有错误.我还尝试在程序末尾添加for {},以便让goroutine有时间打印某些内容,但这无济于事.

I would expect this to print "test" however it simply doesn't do anything, no message but no error either. I've also tried adding a for {} at the end of the program to give the goroutine time to print something but that didn't help.

任何想法可能是什么问题?

Any idea what could be the issue?

推荐答案

程序执行不等待调用的函数完成

program execution does not wait for the invoked function to complete

Go语句

稍等片刻.例如,

package main

import (
    "fmt"
    "time"
)

func test() {
    fmt.Println("test")
}

func main() {
    go test()
    time.Sleep(10 * time.Second)
}

输出:

test

这篇关于简单的goroutine在Windows上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆