Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

go - 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):

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You create new variables inside your if-scopes called year, month, and day:

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 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...