Trying to get the result into a JSON string, I have to use MapScan
because i have no structs that represent the data so here is what i did
import (
"fmt"
"log"
"encoding/json"
_ "github.com/jmoiron/sqlx"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sqlx.Connect("mysql", "uname:pwd@/db")
if err != nil {
log.Fatal(err)
}
m := map[string]interface{}{}
//Go through rows
rows, err := db.Queryx("SELECT id,cname FROM items")
for rows.Next() {
err := rows.MapScan(m)
if err != nil {
log.Fatal(err)
}
}
//Marshal the map
b, _ := json.Marshal(m)
//Prints the resulted json
fmt.Printf("Marshalled data: %s\n", b)
}
The output is Marshalled data: {"cname":"c29tZWl0ZW0","id":"MA=="}
and it should be Marshalled data: {"cname":"someitem","id":0}
and I am not sure how to go around this since the values returned in base64
encodig, any ideas?