I have a problem with formatting when using sqlx
rows, err = db.Queryx(query)
for rows.Next() {
results := make(map[string]interface{})
err = rows.MapScan(results)
fmt.Fprintf(w,"%#v
", results)
}
The output when using %#v
fmt.Fprintf(w,"%#v
", results)
map[string]interface {}{"USER_ID”:”JD”, "USER_NAME”:”John Doe”}
map[string]interface {}{"USER_ID”:”JAD”, "USER_NAME”:”Jane Doe”}
map[string]interface {}{"USER_ID”:”DD”, "USER_NAME”:”Donald Duck”}
Using only %v
fmt.Fprintf(w,"%v
", results)
map[USER_ID:JD USER_NAME:John Doe]
map[USER_ID:JAD USER_NAME:Jane Doe]
map[USER_ID:DD USER_NAME:Donald Duck]
The desired output is to get rid of map[string]interface {}
{"USER_ID”:”JD”, "USER_NAME”:”John Doe”}
{"USER_ID”:”JAD”, "USER_NAME”:”Jane Doe”}
{"USER_ID”:”DD”, "USER_NAME”:”Donald Duck”}
Is this possible?
EDIT USING JSON
According to @Timothy Jones I have updated the main code and it work as expected. Except that the result is printed to the TERMINAL and not browser.
rows, err = db.Queryx(query)
for rows.Next() {
results := make(map[string]interface{})
err = rows.MapScan(results)
if err := enc.Encode(results); err != nil {
fmt.Fprintf(w,"%s
", results)
}
}
{"USER_ID”:”JD”,”USER_NAME”:”John Doe”}
{"USER_ID”:”JAD”,”USER_NAME”:”Jane Doe”}
{"USER_ID”:”DD”,”USER_NAME”:”Donald Duck”}
Removing the error handling just to test. It shows wrong results, but are printed to the BROWSER as it should.
rows, err = db.Queryx(query)
for rows.Next() {
results := make(map[string]interface{})
err = rows.MapScan(results)
enc.Encode(results)
fmt.Fprintf(w,"%s
", results)
}
map[USER_ID:JD USER_NAME:John Doe]
map[USER_ID:JAD USER_NAME:Jane Doe]
map[USER_ID:DD USER_NAME:Donald Duck]
One question remains. How do I print the correct result to the BROWSER?