I have a code like this below.
var sql string
if pnt.Type == "newType" {
sql = `select code, count(*) count from (
select code
from code_table
where start >= ? and end <= ?
union
select code
from code_table
where start >= ? and end <= ?
) a group by code`
rows, err := pnt.readConn("testdb").Query(sql, start, end, start, end)
} else {
sql = `select code, count(*) count from code_table where start >= ? and end <= ?` group by code
rows, err := pnt.readConn("testdb").Query(sql, start, end)
}
if err == nil {
defer rows.Close()
for rows.Next() {
var code, count int
rows.Scan(&code, &count)
}
} else {
log.Println(err)
}
This will give me an error something like this "Variable not declared for rows, err"...
I've tried declaring "var err error" and within the if else statement, I use = instead of := something like this
var err error
rows, err = pnt.switchConn("base", "read").Query(sql, start, end)
However, I still can't declare the rows cuz I will have different kind of errors for that. I tried declaring it as string but no luck.
This is my first time using golang and the if else thing is giving me a hard time, why can't I just use := inside if else statement. As you can see, I can't use rows, err := outside the if else statement, cuz both have different numbers of parameters.