###前提・実現したいこと
dbから取得した値の中に、被っている項目があるので、被っている項目をオブジェクトとし、被っていない値を配列として出力したいと考えています。
ですが、今のところ実現方法がわからないので、ここに書いています。
現在も模索中です...
###該当のソースコード
go
1type Member struct { 2 Id int64 `db:"id" json:"id"` 3 Student string `db:"student" json:"student"` 4 Class string `db:"class" json:"class"` 5}
go
1func GetStudentMember() []Member { 2 query := "SELECT * FROM member" 3 4 var mem []Member 5 _, err := DbMap.Select(&mem, query) 6 CheckErr(err, "Executing query failed") 7 8 return mem 9}
go
1func GetStudentMember(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { 2 R.JSON(w, http.StatusOK, models.GetStuentMember()) 3}
###現在の出力内容
json
1[ 2{ 3"id": 1, 4"student": "タカハシ", 5"class": "桜" 6}, 7{ 8"id": 2, 9"student": "スズキ", 10"class": "桜" 11}, 12{ 13"id": 3, 14"student": "タナカ", 15"class": "桜" 16}, 17{ 18"id": 4, 19"student": "イシバシ", 20"class": "梅" 21}, 22{ 23"id": 5, 24"student": "ナカヤマ", 25"class": "梅" 26} 27] 28 29
####三つ目で全件取得することができているのですが、被っている項目をオブジェクトとして他を配列にできていないません。
###結果的に表示したい内容
json
1{ 2"桜": [ 3{ 4"id": 1, 5"student": "タカハシ" 6}, 7{ 8"id": 2, 9"student": "スズキ" 10}, 11... 12], 13"梅": [ 14{ 15"id": 4, 16"student": "イシバシ" 17}, 18... 19] 20}
###試したこと
sql文のCONCATやmap?等試したのですが、いまいちうまくできていません。
ご教授ください...
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。