0
votes

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.

2
i;ve search from others but couldn't solve the rows problem, I couldn't declare rows until someone show me the solution - nodeffect

2 Answers

1
votes

You are facing issues because of the scope of the variable. In Golang, := creates a new variable inside a scope.

rows, err := pnt.ReadConn("testdb").Query(sql, start, end, start, end)

Creates a new rows and err variables in the if block which won't be accessible outside the if block.

Shorthand declarations in Go

The fix,

var sql string
var err error
var rows *sql.Rows

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)
}
0
votes

In golang ":=" mean you declare a variable and assign them a this value GO will automatically detect his type so : Exemples variable := 15 It’s the same var variable int = 15

So when you do this rows, err := pnt.switchConn("base", "read").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.switchConn("base", "read").Query(sql, start, end) } You declare the same variable rows and err twice