Hi,
I build a simple Api in go, and I’m trying to list some document, like this one
{
"artist": "Tugalo Holler",
"song": "Prince of peace",
"picture": "Tugalo Holler.png",
"mix": "princeofpeace.mp3",
"before": "princeofpeace_before.mp3",
"facebook": "https://www.facebook.com/tugaloholler/",
"leadVocal": "f",
"table": "portfolio"
}
I’m following the example from this blog post : N1QL – Typed and Untyped JSON Schemas in GO
My struct is this one :
package main
import "encoding/json"
type Portfolios []Portfolio
type Portfolio struct { Artist string `json:"artist"` Song string `json:"song"` Picture string `json:"picture"` Mix string `json:"mix"` Before string `json:"before"` Facebook string `json:"facebook"` LeadVocal string `json:"leadVocal"` }
here is the function code :
func portfoliosHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
strQuery := Concat("SELECT * FROM ", getBucket().Name(), " where table = 'portfolio'")
fmt.Println(strQuery)
myQuery := gocb.NewN1qlQuery(strQuery)
myQuery.AdHoc(false)
var row Portfolio
var retValues Portfolios
rows, err := getBucket().ExecuteN1qlQuery(myQuery, nil)
if err != nil {
fmt.Println(err)
http.Error(w, "err", http.StatusInternalServerError)
return
}
for rows.Next(&row) {
fmt.Println(row)
retValues = append(retValues, row)
row = Portfolio{}
}
err = rows.Close()
if err != nil {
fmt.Println(err)
http.Error(w, "err", http.StatusInternalServerError)
return
}
bytes, err := json.Marshal(retValues)
if err != nil {
fmt.Println(err)
http.Error(w, "err", http.StatusInternalServerError)
return
}
w.Write(bytes)
}
The result is actualy :
[{“artist”:"",“song”:"",“picture”:"",“mix”:"",“before”:"",“facebook”:"",“leadVocal”:""}]
I know that the trouble is around the “for” loop, with the “Next” func, because when I replace that
var row Portfolio
var retValues Portfolios
with that :
var row interface{}
var retValues []interface{}
I haven’t the same result :
`[{"ssogo":{"artist":"Tugalo Holler","before":"princeofpeace_before.mp3","facebook":"https://www.facebook.com/tugaloholler/","leadVocal":"f","mix":"princeofpeace.mp3","picture":"Tugalo Holler.png","song":"Prince of peace","table":"portfolio"}}]`
I espected that :
[
{
"artist": "Tugalo Holler",
"song": "Prince of peace",
"picture": "Tugalo Holler.png",
"mix": "princeofpeace.mp3",
"before": "princeofpeace_before.mp3",
"facebook": "https://www.facebook.com/tugaloholler/",
"leadVocal": "f",
"table": "portfolio"
}
]
I don’t understand what is wrong. Any idea ?
Regards
Steeve