Feeding SQL Query Result slice into Struct Feild

I have managed to get my query to feed into a slice variable result:=[]tag{} However im inexperienced with Golang and am unsure as to how to feed the result into the Sum feild.

My code is below any help would be great!

type Tag struct { Sum string json:"sum" Query_desc string json:"Query_Desc" Query_start_date string json:"Query_start_date" Query_end_date string json:"Query_end_date" Current_date string json:"Current_date" Error_info string json:"Error_Info" }

func handler(w http.ResponseWriter, r http.Request) { w.Header().Set(“Access-Control-Allow-Origin”, "") w.Header().Set(“Access-Control-Allow-Headers”, “Origin, X-Requested-With, Content-Type, Accept”)

db, err := sqlx.Connect(“mysql”, “****”)

date_1 := r.FormValue(“date_1”) date_2 := r.FormValue(“date_2”)

var tag Tag

result := []Tag{}

Query := (“SELECT ROUND(SUM(duration)/3600,0) sum FROM sessions WHERE (app_id = ‘idrice’ OR app_id =‘idrios’ OR app_id =‘’ OR **** ='‘OR app_id =’') AND date(created ) between ? and ? and time(created ) between ‘16:00:00’ and ‘18:00:00’ AND ( media_src ='’ OR media_src=‘’ OR media_src ='’ OR media_src=‘’ OR **** ='’ OR ****=‘****’) GROUP BY date(created)”)

err = db.Select(&result, Query, date_1, date_2)

tag.Query_desc = “Listener Hours”
tag.Query_start_date = date_1
tag.Query_end_date = date_2
dt := time.Now()
tag.Current_date = dt.Format(“01-02-2006 15:04:05”)

if err != nil { tag.Error_info = err.Error() }

j, err := json.Marshal(tag) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte((err.Error()))) return } w.Write(j)


func main() { http.HandleFunc(“/”, handler) log.Fatal(http.ListenAndServe(“:8081”, nil)) }

What if you try db.QueryRowx("<SQL here>").Scan(&tag.Sum)?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.