Avoid nesting from conjunction with function that returns 2 values in go?

旧城冷巷雨未停 提交于 2019-12-22 13:33:00

问题


Here, I have a conjunction expression involving some functions that return 2 values:

if _, ok := f(); ok {
  if _, ok := g(); !ok {
    if h() {
      if _, ok := i(); ok {
        doStuff()
      }
    }
  }
}

Could I somehow avoid the nesting? Instead of nesting, could I write this as an expression in one line (I can't quite break or return early in this case)?


回答1:


With a helper function, you can.

Create a helper function which returns the second bool return value, e.g.:

func check(dummy interface{}, ok bool) bool {
    return ok
}

And using it:

if check(f()) && check(g()) && h() && check(i()) {
    doStuff()
}

Note that this is equivalent to the original code because the && operator is evaluated from left to right and it is using short-circuit evaluation: if any of the operands evaluate to false, further operands (functions) will not be called.

This check() function works for all functions that return 2 values and the 2nd is of type bool (because any value can be assigned to a variable of type interface{}).

This is covered in the Spec: Calls:

As a special case, if the return values of a function or method g are equal in number and individually assignable to the parameters of another function or method f, then the call f(g(parameters_of_g)) will invoke f after binding the return values of g to the parameters of f in order. The call of f must contain no parameters other than the call of g, and g must have at least one return value. If f has a final ... parameter, it is assigned the return values of g that remain after assignment of regular parameters.

Note: since the first parameter in check() is not used, we can even use the blank identifier when naming it which will make it obvious that we don't use that parameter:

func check(_ interface{}, ok bool) bool {
    return ok
}



回答2:


Avoid deep nesting or long conditionals running off the right side of the page with a function. For example,

package main

func f() (int, bool) { return 1, true }

func g() (int, bool) { return 1, true }

func h() bool { return true }

func i() (int, bool) { return 1, true }

func doStuff(f, g, i int) int { return f + g + i }

func andDoStuff() {
    ff, ok := f()
    if !ok {
        return
    }
    gg, ok := g()
    if !ok {
        return
    }
    if ok := h(); !ok {
        return
    }
    ii, ok := i()
    if !ok {
        return
    }
    doStuff(ff, gg, ii)

}

func main() {
    andDoStuff()
}


来源:https://stackoverflow.com/questions/35548073/avoid-nesting-from-conjunction-with-function-that-returns-2-values-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!