Search code examples
sqlitegobeego

Beego raw sql - Type conversion issue


Here is the code used inside the Beego MVC architecture.

var maps []orm.Params
//Pallets Completed already.
o.Raw("Select SUM(Things) as AllTheThings FROM SomeTable").Values(&maps)

numThings := strconv.Atoi(maps[0]["AllTheThings"].(string))

c.Data["Stuff"] = maps[0]["AllTheThings"]

Error: multiple-value strconv.Atoi() in single-value context

Trying to figure out how I can get data out with our ORM and type cast it so arithmetic can be done on it.

Any more details please let me know.


Solution

  • strconv.Atoi has a signature of:

    func Atoi(s string) (int, error)
    

    you should check the error before using the result, like so:

    var maps []orm.Params
    //Pallets Completed already.
    o.Raw("Select SUM(Things) as AllTheThings FROM SomeTable").Values(&maps)
    
    numThings, err := strconv.Atoi(maps[0]["AllTheThings"].(string))
    if err != nil {
        // couldn't convert
    }
    
    c.Data["Stuff"] = maps[0]["AllTheThings"]