去 - 宣布和不使用的错误,当我想我已经这样做的变量 [英] Go — declared and not used error, when I think I have done so to the variable
本文介绍了去 - 宣布和不使用的错误,当我想我已经这样做的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这段代码有什么问题?
包主
$ b导入fmt
// fibonacci是一个返回
的函数//一个返回int的函数。
func fibonacci()func()int {
prev:= 0
curr:= 1
return func()int {
temp:= curr
curr:= curr + prev
prev:= temp
return curr
}
}
func main(){
f:=斐波那契()
为i:= 0;我< 10; i ++ {
fmt.Println(f())
}
}
prog.go:13:prev已声明且未使用
解决方案
命名为 prev
,然后从未使用它。
具体来说,您说 prev:= temp
。这是在当前作用域 prev
中创建一个新的局部变量。我假设你打算说 prev = temp
,它修改了从周围范围继承的 prev
变量。同样,你可能在上一行说的是 curr = curr + prev
,而不是使用:=
。 p>
What's wrong with this code?
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
prev := 0
curr := 1
return func() int {
temp := curr
curr := curr + prev
prev := temp
return curr
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
prog.go:13: prev declared and not used
解决方案
You declared a variable named prev
and then never used it.
Specifically, you said prev := temp
. This is creating a new local variable in the current scope named prev
. I assume you meant to just say prev = temp
, which modifies the prev
variable inherited from the surrounding scope. Similarly you probably meant to say curr = curr + prev
on the previous line, instead of using :=
.
这篇关于去 - 宣布和不使用的错误,当我想我已经这样做的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文