0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GinのBindJSONでデータが取れない問題

0
Posted at

結論

BindJSONしようとしているstructのfield(?)はpublicにしておかなければならないようです。
struct宣言とリクエストハンドラ宣言が同じpackage内だったとしても!

type User struct {
//	name    string	-> ダメ
	Name    string
}

// Post POST method
func Post(ctx *gin.Context) {
	var user User
	ctx.BindJSON(&user)
	fmt.Println("name:", user.Name)
}

考察

もしかしたらstructがBindJSON()に渡されるとき、fieldがpublicじゃないとgin側から見えないのかな(??)
みたいな?

あとがき

この問題について、参考に貼ったissueくらいで、日本語のものが見つからなかったので書きました。
くだらない記事ですが、初心者同志の手助けになれば幸いです。

参考

解決のきっかけとなったissue

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?