struct
のフィールド値がnil
であるかどうかを確認するきれいな方法があれば、structのフィールドがnilであるかどうか確認してください
私は
type test_struct struct {
Name string `json:"name" binding:"required"`
Email string `json:"email" binding:"required"`
Message string `json:"message" binding:"required"`
}
そしてGin
と私は
var temp test_struct
c.Bind(&temp)
ような値でstruct
を満たしていますし、すべてが正常に動作しますが、私はtemp.Name
のいずれかどうかを確認したい、temp.Email
、temp.Message
を持っていると仮定nil
です。nil
:if temp.Name == nil
などと各フィールドを比較するだけで確認できますが、私はきれいなものを探していますそのバージョンはありますか?
UPDは:原因Go
言語での知識の欠如に私たちはbinding:"required"
フィールドでstrucureを渡すとgin
パッケージのBind
関数がエラーを返すことを知りませんでした。 @ matt.sに感謝します。だから、答えはerr
をチェックするために、次のようになります。
var temp test_struct
err := c.Bind(&temp)
if err != nil {
// Do my stuff(some of fields are not set)
}
文字列は移動しないでください。 –