英文:
Go compiler says "declared and not used" but they are being used
问题
我有以下的函数,它给我返回“变量声明但未使用”的错误:
type Comparison struct {
Left []byte
Right []byte
Name string
}
func img(w http.ResponseWriter, r *http.Request, c appengine.Context, u *user.User) {
key := datastore.NewKey("Comparison", r.FormValue("id"), 0, nil)
side := r.FormValue("side")
comparison := new(Comparison)
err := datastore.Get(c, key, comparison)
check(err)
if( side == "left"){
m, _, err := image.Decode(bytes.NewBuffer(comparison.Left))
} else {
m, _, err := image.Decode(bytes.NewBuffer(comparison.Right))
}
check(err)
w.Header().Set("Content-type", "image/jpeg")
jpeg.Encode(w, m, nil)
}
它给我返回以下错误:
dpcompare.go:171: m声明但未使用
dpcompare.go:171: err声明但未使用
dpcompare.go:173: m声明但未使用
dpcompare.go:173: err声明但未使用
dpcompare.go:178: 未定义:m
dpcompare.go:185: key声明但未使用
问题是m
,err
和key
都被使用了。我无法理解为什么编译器认为它们没有被使用。
英文:
I have the following function that is giving me "variable declared and not used" errors:
type Comparison struct {
Left []byte
Right []byte
Name string
}
func img(w http.ResponseWriter, r *http.Request, c appengine.Context, u *user.User) {
key := datastore.NewKey("Comparison", r.FormValue("id"), 0, nil)
side := r.FormValue("side")
comparison := new(Comparison)
err := datastore.Get(c, key, comparison)
check(err)
if( side == "left"){
m, _, err := image.Decode(bytes.NewBuffer(comparison.Left))
} else {
m, _, err := image.Decode(bytes.NewBuffer(comparison.Right))
}
check(err)
w.Header().Set("Content-type", "image/jpeg")
jpeg.Encode(w, m, nil)
}
It gives me the following errors:
dpcompare.go:171: m declared and not used
dpcompare.go:171: err declared and not used
dpcompare.go:173: m declared and not used
dpcompare.go:173: err declared and not used
dpcompare.go:178: undefined: m
dpcompare.go:185: key declared and not used
The thing is m
, err
, and key
are all being used. I can't wrap my head around why the compiler thinks thy are not.
答案1
得分: 5
如@kostix所说,m
是在if
的作用域内。尝试以下代码
type Comparison struct {
Left []byte
Right []byte
Name string
}
func img(w http.ResponseWriter, r *http.Request, c appengine.Context, u *user.User) {
key := datastore.NewKey("Comparison", r.FormValue("id"), 0, nil)
side := r.FormValue("side")
comparison := new(Comparison)
err := datastore.Get(c, key, comparison)
check(err)
// 注意!现在m在函数的作用域内
var m Image
if( side == "left"){
m, _, err = image.Decode(bytes.NewBuffer(comparison.Left))
} else {
m, _, err = image.Decode(bytes.NewBuffer(comparison.Right))
}
check(err)
w.Header().Set("Content-type", "image/jpeg")
jpeg.Encode(w, m, nil)
}
英文:
As @kostix said, m
is local to the scope of the if
. Try this code
type Comparison struct {
Left []byte
Right []byte
Name string
}
func img(w http.ResponseWriter, r *http.Request, c appengine.Context, u *user.User) {
key := datastore.NewKey("Comparison", r.FormValue("id"), 0, nil)
side := r.FormValue("side")
comparison := new(Comparison)
err := datastore.Get(c, key, comparison)
check(err)
// NOTE! now m is in the function's scope
var m Image
if( side == "left"){
m, _, err = image.Decode(bytes.NewBuffer(comparison.Left))
} else {
m, _, err = image.Decode(bytes.NewBuffer(comparison.Right))
}
check(err)
w.Header().Set("Content-type", "image/jpeg")
jpeg.Encode(w, m, nil)
}
答案2
得分: 2
我认为你在那些if
分支中声明的变量是局部变量,只在这些分支的代码块中有效。这不是JavaScript(幸运的是)。所以只需在if
之前的某个地方声明变量,并使用=
而不是:=
来赋值给它们。
英文:
I think the variables you declare in those if
branches are local to the code blocks of these branches. This is not JavaScript (luckily). So just declare your variables somewhere above if
and use =
instead of :=
to assign to them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论