youbbs
youbbs
6799 1 0

把JSON数据转为Go struct

一个在线懒人工具,把json 转换为go struct 结构,效果如下图

前往试试 JSON-to-Go https://mholt.github.io/json-to-go/

0

See Also

Nearby


Discussion (1)

getkokomi
getkokomi 2018-11-19 09:45
//UserUpdate 更新用户
func UserUpdate(_db *db.DB, obj User) error {
	jb, _ := json.Marshal(obj)
	return _db.Hset("user", db.I2b(obj.ID), jb)
}

//UserGetByID  通过ID获取用户信息
func UserGetByID(_db *db.DB, uid uint64) (User, error) {
	obj := User{}
	rs := _db.Hget("user", db.I2b(uid))
	if rs.State == "ok" {
		json.Unmarshal(rs.Data[0], &obj)
		return obj, nil
	}
	return obj, errors.New(rs.State)
}

//UserGetByName 通过name获取用户信息
func UserGetByName(_db *db.DB, name string) (User, error) {
	obj := User{}
	rs := _db.Hget("user_name2uid", []byte(name))
	if rs.State == "ok" {
		rs2 := _db.Hget("user", rs.Data[0])
		if rs2.State == "ok" {
			json.Unmarshal(rs2.Data[0], &obj)
			return obj, nil
		}
		return obj, errors.New(rs2.State)
	}
	return obj, errors.New(rs.State)
}

//UserGetIDByName 通过name获取用户ID
func UserGetIDByName(_db *db.DB, name string) string {
	rs := _db.Hget("user_name2uid", []byte(name))
	if rs.State == "ok" {
		return db.B2ds(rs.Data[0])
	}
	return ""
}
0
Login Topics