0

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
}
0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You're looking for the terms "callee" and "caller", not "child" and "parent".
@rightfold I'm not looking for those (I know them), I was just using the asker's terms hence I put them in quotes.
@rightfold But you're right, it would be better to put aside asker's terms and use the right ones. So I edited, thanks.
0

Use the return statement:

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

    apiResponse("all good")
}

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

3 Comments

Is it possible to simplify code and return FROM a func? I'll have plenty of apiResponse() calls and it can be easy to forget return
@Andrey no, luckily not. That would be a readability disaster.
@Andrey If apiResponse() returns an error and apiEndpoint() returns an error, the compiler will enforce an explicit return. You could then do return apiResponse() - which will call the function and return the error value (if any), else nil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.