dtt5830659 2014-07-03 05:30
浏览 13
已采纳

是否可以从另一个功能“返回”一个功能?

Due the verbose error handling syntax I've created a function check as below which acts as a "global" error handler. If I want to panic instead to log I change only the check function. The issue is that now I want to simply return so that the other go routines can still run if one has an error. So the question is: How can I do that ? Is it possible?

func main() {
    for k, v := range foo {
        go func() {
            err = doSomething()
            check("this one failed", err)
        }()
    }
}

func check(errMsg string, err error) {
    if err != nil {
        log.Fatalf(errMsg, err)
    }
}

However now I've found that I need return the anonymous function if there is any error rather than exit ( log.Fatal ) so I'm wondering if it's possible to return the anyonymou

  • 写回答

2条回答 默认 最新

  • doukang8949 2014-07-03 07:06
    关注

    There isn't a language feature that allows you to cause the parent function to automatically return in response to a simple function call.

    However, there is a way to cause the current goroutine to exit, which might be what you are after: runtime.Goexit. Note that this has similar disruptive potential to calling os.Exit, so it would be bad to call it in the context of goroutines created by other packages or other unrelated code.

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

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接