0
点赞
收藏
分享

微信扫一扫

解决go数据表查询结构体对应字段null问题(sqlx converting NULL to string is unsupported)

悬灸人雪洋 2022-03-25 阅读 61
golang

将结构体对应字段类型设为指针类型,一劳永逸,不用担心json序列化与反序列化问题

// User 用户结构体(对应mysql表)
type User struct {
	Id         int       `db:"id"`
	Code       *string   `db:"code"`
	NickName   string    `db:"nick_name"`
	Phone      string    `db:"phone"`
	Password   string    `db:"password"`
	CreateTime time.Time `db:"create_time"`
	Sex        int       `db:"sex"`
	Email      string    `db:"email"`
}

例如上述的Code字段,不设置为指针类型的话,查询时mysql里为null的话序列化就会出现“sqlx converting NULL to string is unsupported”

举报

相关推荐

0 条评论