GO err is shadowed during return

这里先复现问题,然后进行问题说明。

//test.go

package main


import "fmt"
import "strconv"


func foo(x string) (ret int, err error) {
	if true {
		ret, err := strconv.Atoi(x)
		if err != nil {
			return
		}
	}
	return ret, nil
}


func main() {
	fmt.Println(foo("123"))
}


运行:

GO err is shadowed during return_第1张图片


OK,问题复现了,下面进行问题分析。

func foo(x string) (ret int, err error) {//返回值列表定义了ret和err变量,作用域是整个函数体
if true {//新的语句块
ret, err := strconv.Atoi(x)  //这里又定义了新的变量ret和err,和返回值列表重名了。作用域是if语句块
if err != nil {
return  //这里的return语句会导致外层的ret和err被返回,而不是if语句里的ret和err
}
}
return ret, nil
}

来自网上的解释:

It's a new scope, so a naked return returns the outer err, not your inner err that was != nil.
So it's almost certainly not what you meant, hence the error.
下面进行修改(只需要保证局部变量和全局变量不重名即可):

//test.go

package main


import "fmt"
import "strconv"


func foo(x string) (ret int, err error) {
if true {
ret1, err1 := strconv.Atoi(x)
if err1 != nil {
err = err1
return
}
ret = ret1
}


return ret, nil
}


func main() {
fmt.Println(foo("123"))
}

运行:



你可能感兴趣的:(go,go)