How to return from a function by a closure function?

Please look at this code

func a(){
    for {
        function := func(){
               return //i want to exit the function "a" right now
          }
    }
}

i wanna exit the loop(function) on the first loop or whatever round it is. so how can i handle this type of situation when i have to return from a looped function by a closure function?

Your example doesn’t call the function in question, so it’s hard to say. But generally, by returning something that the caller can act on:

func a() {
    fn := func(n int) bool {
        if n > 10 {
            return false
        }
        return true
    }

    i := 0
    for {
        if !fn(i) {
            break
        }
        i++
    }
}
3 Likes

This example is tricky. And it worked for me. Thanks a lot.

1 Like