失踪的回报让我疯狂。

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

Go missing returns are driving me crazy

问题

我正在尝试学习Go语言,除了函数的返回语句,其他都进行得很顺利,但是我无论如何都无法理解返回语句。在一本书的练习中,提出了构建一个函数,将一个整数减半并返回减半后的整数以及是否为偶数或奇数(减半后的整数)的布尔值。对此没有问题,以下是相关的代码:

func checker(input int) (int, bool){
    if (((input /2) % 2) == 0) {
        return input / 2, true
    } else {
        return input / 2, false
    }
}

但是当我尝试将其转换为接受切片的函数时,我发誓我无法理解返回语句的缺失。以下是相关的代码:

func checker(args ...int) (int, bool){
    for _, v := range args {
        if (((v /2) % 2) == 0) {
            return v / 2, true
        } else {
            return v / 2, false
        }
    }
}

func main () {
    xs := []int{3,45,6,32,43,76,42,4,77,8}
    fmt.Println(checker(xs...))
}

我现在认为我理解了我提出的代码的几个问题,感谢您的回答。

英文:

I'm trying to learn Go and it's going very well except for the functions return statements, which I cannot for my life get a grasp on.
In an exercise in a book it is proposed to construct a function that halves an int and return the halved int and if even or odd (the halved) with a bool. No problems with that, here is the relevant code.

func checker(input int) (int, bool){
    if (((input /2) % 2) == 0) {
        return input / 2, true
    } else {
        return input / 2, false
    }
}

But when trying to convert this to accept a slice I swear I cannot understand where the returns are missing. Here again is the relevant code for that:

func checker(args ...int) (int, bool){
    for _, v := range args {
        if (((v /2) % 2) == 0) {
            return v / 2, true
        } else {
            return v / 2, false
        }
    }
}

func main () {
    xs := []int{3,45,6,32,43,76,42,4,77,8}
    fmt.Println(checker(xs...))
}

I think now I understand several of the problems of the code I presented, thank you for your responses.

答案1

得分: 6

checker()函数没有保证会接收到多于零个参数,在这种情况下,循环体将不会执行,因此不会到达任何return语句。

因此,编译器有权对缺少返回值进行投诉,因为这个条件是在运行时决定的。

例如,如果你传递了0个参数:checker(),这是有效的,如果不要求return语句,就会引发问题。

因此,只需添加一个带有合理返回值的return语句,通常是结果类型的零值:

func checker(args ...int) (int, bool){
    for _, v := range args {
        if (((v /2) % 2) == 0) {
            return v / 2, true
        } else {
            return v / 2, false
        }
    }
    return 0, false
}

还要注意,这个循环只检查第一个传入的参数并返回,可能不是你想要的结果。

英文:

There is no guarantee checker() gets more than zero arguments, in which case the loop body would be executed zero times, so no return statements would be reached.

So the compiler is rightful to complain about a missing return, as this condition is decided at runtime.

E.g. if you pass 0 arguments: checker(), it's valid and would cause problem if a return would not be demanded.

So simply add a return statement with reasonable return values, often the zero values of the result types:

func checker(args ...int) (int, bool){
    for _, v := range args {
        if (((v /2) % 2) == 0) {
            return v / 2, true
        } else {
            return v / 2, false
        }
    }
    return 0, false
}

Also note that this loop only checks the first passed argument and returns, probably not what you want.

答案2

得分: 1

你只在for循环范围内返回。在for循环之后添加一个返回语句。如果你的参数为空,那么你的循环不会运行,也不会有返回值。所以你需要在for循环之后添加一个返回语句。

我建议将此处理为错误场景或其他情况。下面我提供了一个简单的解决方案。

func checker(args ...int) (int, bool){
    for _, v := range args {
        if ((v /2) % 2 == 0) {
            return v / 2, true
        } else {
            return v / 2, false
        }
    }
    return 0, false
}
英文:

You return only in the For loop Scope. Return after the for loop. If your args are empty, then your loop does not run and no returns reached. So you need to add a return after for loop.

I suggest handle this as an error scenario or something. Below I have mention the simple solution for this.

func checker(args ...int) (int, bool){
    for _, v := range args {
        if ((v /2) % 2 == 0) {
            return v / 2, true
        } else {
            return v / 2, false
        }
    }
    return 0, false
}

huangapple
  • 本文由 发表于 2021年6月8日 20:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/67886478.html
匿名

发表评论

匿名网友

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

确定