简体   繁体   English

已声明但未使用的变量

[英]variable declared but not used

I have tried different strategies to no avail.我尝试了不同的策略无济于事。 Following code in vscode shows variable declared but not used for year , month and day while they are used in casting (last 3 lines of code): vscode中的以下代码显示了声明但未用于yearmonthday的变量,而它们用于强制转换(最后 3 行代码):

var year, month, day int
year = -1
month = -1
day = -1
// Calculate Year Month Day
if eventCalendar == "gregorian" {
    s := strings.Split("eventDate", "/")
    year, err := strconv.Atoi(s[0])
    if err != nil {
        log.Fatal("Cannot convert year to integer: " + s[0] + ". " + err.Error())
    }
    month, err := strconv.Atoi(s[1])
    if err != nil {
        log.Fatal("Cannot convert month to integer: " + s[1] + ". " + err.Error())
    }
    day, err := strconv.Atoi(s[2])
    if err != nil {
        log.Fatal("Cannot convert day to integer: " + s[2] + ". " + err.Error())
    }
} else if eventCalendar == "jalali" {
    s := strings.Split("eventDate", "-")
    year, err := strconv.Atoi(s[0])
    if err != nil {
        log.Fatal("Cannot convert year to integer: " + s[0] + ". " + err.Error())
    }
    month, err := strconv.Atoi(s[1])
    if err != nil {
        log.Fatal("Cannot convert month to integer: " + s[1] + ". " + err.Error())
    }
    day, err := strconv.Atoi(s[2])
    if err != nil {
        log.Fatal("Cannot convert day to integer: " + s[2] + ". " + err.Error())
    }
    // TODO: Convert to gregorian
} else {
    panic("Unknown calendar type: eventcalendar")
}
strYear := strconv.Itoa(year)
strMonth := strconv.Itoa(month)
strDay := strconv.Itoa(day)

//... rest of code //... rest 的代码

You create new variables inside your if-scopes called year, month, and day:您在 if 范围内创建新变量,称为年、月和日:

year, err := strconv.Atoi(s[0])

The := is the problem here. :=是这里的问题。 At the beginning add a var err error to your code and remove the colon from your function calls:在开始时向您的代码添加一个var err error ,并从您的 function 调用中删除冒号:

var year, month, day int
var err error
year = -1
month = -1
day = -1
// ...
    year, err = strconv.Atoi(s[0])
    // ...

I believe this should fix your problem.我相信这应该可以解决您的问题。 Right now you are creating year, month, and day in the if-scope and never use them (inside the scope).现在,您正在 if 范围内创建年、月和日,并且从不使用它们(在范围内)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM