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

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

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

问题

我有以下的Go示例代码:

package main

import (
	"fmt"
	"log"
	"os"
	"strconv"
)

func main() {
	numberOfUsers := 10
	numberOfUsersStr, found := os.LookupEnv("NUMBER_OF_USERS")
	if found {
		numberOfUsers, err := strconv.Atoi(numberOfUsersStr)
		if err != nil {
			log.Fatalln(err)
		}
	}
	fmt.Printf("Number of users: %d", numberOfUsers)
}

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

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

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

英文:

I have the following Go example:

package main

import (
	"fmt"
	"log"
	"os"
	"strconv"
)

func main() {
	numberOfUsers := 10
	numberOfUsersStr, found := os.LookupEnv("NUMBER_OF_USERS")
	if found {
		numberOfUsers, err := strconv.Atoi(numberOfUsersStr)
		if err != nil {
			log.Fatalln(err)
		}
	}
	fmt.Printf("Number of users: %d", numberOfUsers)
}

When building this snipper, I get the following error:

> go build -o app .
.\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

numberOfUsers, err := strconv.Atoi(numberOfUsersStr)

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

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

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

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

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.

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

答案2

得分: 0

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

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

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

func main() {
    numberOfUsers := 10
    numberOfUsersStr, found := os.LookupEnv("NUMBER_OF_USERS")
    if found {
        var err error
        numberOfUsers, err = strconv.Atoi(numberOfUsersStr)
        if err != nil {
            log.Fatalln(err)
        }
    }
    fmt.Printf("用户数量:%d", numberOfUsers)
}

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

英文:

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:

func main() {
    numberOfUsers := 10
    numberOfUsersStr, found := os.LookupEnv("NUMBER_OF_USERS")
    if found {
        var err error
        numberOfUsers, err = strconv.Atoi(numberOfUsersStr)
        if err != nil {
            log.Fatalln(err)
        }
    }
    fmt.Printf("Number of users: %d", numberOfUsers)
}

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:

确定