我很困惑如何从函数返回值。我正在尝试以下示例:
因此,函数应返回字符串“ true”和布尔值true,但它返回字符串“ false”和布尔值false
package main
import (
"fmt"
)
func a(c bool) (string, bool) {
if c {
return "true", true
}
a(true)
return "false", false
}
func main() {
s, ok := a(false)
fmt.Println(s, ok)
}
如果c
为false ,则递归代码将始终返回“ false” 。请在下面查看正确的代码。
package main
import (
"fmt"
)
func a(c bool) (string, bool) {
if c {
return "true", true
}
return a(true)
}
func main() {
s, ok := a(false)
fmt.Println(s, ok)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句