Go编译错误:已声明但未使用(尽管已使用)

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

Go Compilation Error: Declared but not used (Although used)

问题

我有以下的Go示例代码:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strconv"
  7. )
  8. func main() {
  9. numberOfUsers := 10
  10. numberOfUsersStr, found := os.LookupEnv("NUMBER_OF_USERS")
  11. if found {
  12. numberOfUsers, err := strconv.Atoi(numberOfUsersStr)
  13. if err != nil {
  14. log.Fatalln(err)
  15. }
  16. }
  17. fmt.Printf("Number of users: %d", numberOfUsers)
  18. }

在构建这段代码时,我遇到了以下错误:

  1. > go build -o app .
  2. .\app.go:14:3: numberOfUsers declared but not used

显然,该变量在最后的打印语句中被使用了,但它似乎被编译器隐藏了。我错过了什么?

英文:

I have the following Go example:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strconv"
  7. )
  8. func main() {
  9. numberOfUsers := 10
  10. numberOfUsersStr, found := os.LookupEnv("NUMBER_OF_USERS")
  11. if found {
  12. numberOfUsers, err := strconv.Atoi(numberOfUsersStr)
  13. if err != nil {
  14. log.Fatalln(err)
  15. }
  16. }
  17. fmt.Printf("Number of users: %d", numberOfUsers)
  18. }

When building this snipper, I get the following error:

  1. > go build -o app .
  2. .\app.go:14:3: numberOfUsers declared but not used

Clearly the variable is used in the last print statement, however it seems hidden from the compiler. What am I missing?

答案1

得分: 1

当使用:=时,你正在声明一个新的变量。这意味着这里的numberOfUsers

  1. numberOfUsers, err := strconv.Atoi(numberOfUsersStr)

实际上是遮蔽了你的另一个numberOfUsers变量。

你可以通过事先声明err,然后使用=而不是:=来修复它,这样你只是给变量赋一个新值,而不是声明一个新变量。

  1. var err error
  2. numberOfUsers, err = strconv.Atoi(numberOfUsersStr)
英文:

When using :=, you're declaring a new variable. That means the numberOfUsers here:

  1. numberOfUsers, err := strconv.Atoi(numberOfUsersStr)

is actually shadowing your other numberOfUsers variable.

You can fix it by declaring err beforehand and then using just = instead of := so that you are only assigning a new value to the variable and not declaring a new variable.

  1. var err error
  2. numberOfUsers, err = strconv.Atoi(numberOfUsersStr)

答案2

得分: 0

这行代码中的numberOfUsers变量未被使用:numberOfUsers, err := strconv.Atoi(numberOfUsersStr)

在这里,你在if语句的作用域中重新声明了一个新的变量numberOfUsers。这个变量在该作用域中没有被使用,因此会出现错误。

你可能想要像这样修改代码:

  1. func main() {
  2. numberOfUsers := 10
  3. numberOfUsersStr, found := os.LookupEnv("NUMBER_OF_USERS")
  4. if found {
  5. var err error
  6. numberOfUsers, err = strconv.Atoi(numberOfUsersStr)
  7. if err != nil {
  8. log.Fatalln(err)
  9. }
  10. }
  11. fmt.Printf("用户数量:%d", numberOfUsers)
  12. }

请注意:=(声明并赋值一个新变量)和=(仅赋值)之间的区别。

英文:

The numberOfUsers variable in this line is unused: numberOfUsers, err := strconv.Atoi(numberOfUsersStr).

Here you are redeclaring a new variable numberOfUsers in the scope of the if statement. This variable is not used in that scope, hence the error.

You probably want something like this:

  1. func main() {
  2. numberOfUsers := 10
  3. numberOfUsersStr, found := os.LookupEnv("NUMBER_OF_USERS")
  4. if found {
  5. var err error
  6. numberOfUsers, err = strconv.Atoi(numberOfUsersStr)
  7. if err != nil {
  8. log.Fatalln(err)
  9. }
  10. }
  11. fmt.Printf("Number of users: %d", numberOfUsers)
  12. }

Notice the difference between := (declare an assign a new variable) and = (only assign).

huangapple
  • 本文由 发表于 2022年8月15日 22:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/73362133.html
匿名

发表评论

匿名网友

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

确定