如何从一个函数中返回到另一个函数?

huangapple go评论68阅读模式
英文:

How to return in func FROM another func?

问题

我想在调用或退出子函数apiResponse()时,在父函数apiEndpoint()中结束执行。

func apiEndpoint() {
    if false {
        apiResponse("error")
        // 我希望apiResponse()调用能够在父函数中返回(结束执行)
        // 这样下一个apiResponse("all good")就不会被执行
    }

    apiResponse("all good")
}

func apiResponse(message string) {
    // 通过JSON将消息返回给用户
}
英文:

I want to end execution in parent func apiEndpoint() upon calling/exiting in a child func apiResponse()

func apiEndpoint() {
    if false {
        apiResponse("error")
        // I want apiResponse() call to return (end execution) in parent func
        // so next apiResponse("all good") wont be executed
    }

    apiResponse("all good")
}

func apiResponse(message string) {
    // returns message to user via JSON
}

答案1

得分: 3

一个函数或方法无法控制从它被调用的地方执行(控制流)。你甚至不能保证它是从你的函数中调用的,它可能被调用来初始化一个全局变量,例如。

也就是说,结束执行并返回的责任在调用者身上,可以通过使用return语句来显式地结束执行。

如果你的例子像你的这样简单,你可以通过使用if-else来避免使用return语句:

func apiEndpoint() {
    if someCondition {
        apiResponse("error")
    } else {
        apiResponse("all good")
    }
}

另外,如果函数有返回值,并且apiResponse()会返回一个值作为调用者的返回值,你可以在一行中使用return语句,例如:

func apiEndpoint() int {
    if someCondition {
        return apiResponse("error")
    }

    return apiResponse("all good")
}

func apiResponse(message string) int {
    return 1 // 返回一个整数
}

注意:

这只是为了完整性而提及,但不是你的情况下的解决方案:如果被调用的函数会引发panic(),则调用者函数中的执行将停止,并且panic序列将在调用层次结构中向上传递(在运行defer函数之后,如果它们不调用recover())。Panic-recover的设计目的不是为了让被调用函数停止在调用者函数中执行。

英文:

A function or method cannot control the execution (control flow) from where it was called from. You don't even have guarantee it was called from your function, it may be called to initialize a global variable for example.

That being said it is the responsibility of the caller to end the execution and return, explicitly with the return statement.

If the example is as simple as yours, you can avoid the return statement though by using if-else:

func apiEndpoint() {
    if someCondition {
        apiResponse("error")
    } else {
        apiResponse("all good")
    }
}

Also if the functions have return values and the apiResponse() would return a value that would be the return value of the caller, you can do the return in one line, e.g.

func apiEndpoint() int {
    if someCondition {
        return apiResponse("error")
    }

    return apiResponse("all good")
}

func apiResponse(message string) int {
    return 1 // Return an int
}

Note:

Just for completeness but not as a solution in your case: if the callee function would panic(), the execution in the caller function would stop and the panicing sequence would go up in the call hierarchy (after running defer functions, and if they don't call recover()). Panic-recover is designed for something else and not as a mean for callee functions to stop executions in caller functions.

答案2

得分: 0

使用return语句:

func apiEndpoint() {
    if false {
        apiResponse("error")
        return
    }

    apiResponse("all good")
}

func apiResponse(message string) {
    // 通过 JSON 将消息返回给用户
}

请注意,以上是给定的代码示例,其中的内容已经翻译完成。

英文:

Use the return statement:

func apiEndpoint() {
    if false {
        apiResponse("error")
        return
    }

    apiResponse("all good")
}

func apiResponse(message string) {
    // returns message to user via JSON
}

huangapple
  • 本文由 发表于 2015年6月16日 12:30:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/30858823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定