Golangを勉強しています。
Ginを利用してエラーをJSONで返す時に
GO
1error.go 2 3type Error struct { 4 Code int `json:"code"` 5 Message string `json:"message"` 6 UserTitle string `json:"user_title"` 7 UserMessage string `json:"user_message"` 8 Description string `json:"description"` 9 Type string `json:"type"` 10} 11 12func Unknown(description, types string) *Error { 13 return &Error{ 14 Code: 500, 15 Message: "Unknown error occurred", 16 Description: description, 17 Type: types, 18 } 19}
GO
1main.go 2 3if err != nil { 4 c.JSON(http.StatusBadRequest, error.Unknown()) 5 return 6 }
となっているときに返ってくるJSONは
Javascript
1{ 2 "code": 2, 3 "message": "", 4 "user_title": "", 5 "user_message": "", 6 "description": "", 7 "type": "register" 8}
となりますが、これを
Javascript
1{ 2 "error": { 3 "code": 2, 4 "message": "", 5 "user_title": "", 6 "user_message": "", 7 "description": "", 8 "type": "" 9 } 10}
こうしたいのですが、Goの場合、どうやったら実現できるのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/24 06:51