Article directory
need
- Store request data in database
- Return with data read from database
- When deserialization is not used when exporting the library
请求结构体
type NoticegroupsCreateReq struct {
Name string `json:"name" binding:"required"`
UserIds []string `json:"user_ids" binding:"required"`
}
数据库类型
name | type |
---|---|
name | varchar |
user_ids | longtext |
返回结构体
type NoticegroupsCreateReq struct {
Name string `json:"name" binding:"required"`
UserIds []string `json:"user_ids" binding:"required"`
}
code
Warehouse
// []string -> string []userIds -> ids
// 此时输出一下将要入口的数据,待转化数据
fmt.Println(req.UserIds)
// [31 62 32 44 33]
userIds, err := json.Marshal(req.UserIds)
ids := string(userIds)
fmt.Println(string(userIds))
// ["31","62","32","44","33"]
out of warehouse
// string -> []string noticegroup.UserIds -> []userId
// 此时输出一下从数据库直接读到的数据,待转化数据
fmt.Println("s:",noticegroup.UserIds)
// s ="[\"31\"", "\"62\"","\"32\"","\"44\"","\"33\"]"
// 去掉字符串两边的方括号
noticegroup.UserIds = strings.Trim(noticegroup.UserIds, "[]")
// 按逗号拆分字符串
userId := strings.Split(noticegroup.UserIds, ",")
// 去掉每个字符串两边的引号和空格
for i := 0; i < len(userId); i++ {
userId[i] = strings.Trim(userId[i], "\" ")
}
fmt.Println(userId)
// [31 62 32 44 33]
历程
ask | Convert | storage | return |
---|---|---|---|
[31 62] | [“31”,“62”] | "[“31"”, ““62"”]” | [31 62] |