Recently, I'm learning about Go (Golang). I'm trying to make a simple web service using Martini and jwt-go. I didn't find any difficulty in retrieving a single row data and put in JSON as the response. But, when dealing with multiple-rows, it's a whole different story. Basically, I refer to the accepted answer here.
Here is the piece of my code:
m.Get("/users", func(params martini.Params, r render.Render) {
db, err := sql.Open("mysql", "root:@/sirat_v2")
if err != nil {
panic(err.Error())
}
defer db.Close()
rows, err := db.Query("SELECT user_id, nama FROM `users` WHERE password = ?", "asdfasdf")
defer rows.Close()
cols, err := rows.Columns()
if err != nil {
panic(err.Error())
}
partages := make([]*Partage, 0, 10)
var user_id int
var nama string
for rows.Next() {
err = rows.Scan(&user_id, &nama)
if err != nil { /* error handling */
}
partages = append(partages, &Partage{user_id, nama})
}
})
When trying to build, there's an error said that Partage is undefined.