Go编译器报错“声明但未使用”,但它们确实被使用了。

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

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声明但未使用

问题是merrkey都被使用了。我无法理解为什么编译器认为它们没有被使用。

英文:

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.

huangapple
  • 本文由 发表于 2011年5月30日 22:55:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/6177815.html
匿名

发表评论

匿名网友

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

确定