package main
import (
"encoding/json"
"fmt"
)
type List struct {
Content string `json:"content,omitempty"`
ToUser struct {
ID int `json:"id"`
} `json:"to_user,omitempty"`
}
func main() {
var list []List
list = append(list, List{
Content: "hello world",
}, List{
ToUser: , //在这个环节赋值,我不知道怎么处理了
})
res, err := json.Marshal(list)
if err != nil {
panic(err)
}
fmt.Println(string(res))
}
1
rrfeng 2023-03-06 12:16:57 +08:00
ToUser: struct {
ID int `json:"id"` }{ ID: 10, } |
2
ni9ht 2023-03-06 12:21:41 +08:00
```go
// 1 list = append(list, List{ Content: "hello world", }, List{ ToUser: struct { ID int `json:"id"` }{ ID: 1, }, }) // 2 item := List{ Content: "hello world", } item.ToUser.ID = 1 list = append(list, List{ Content: "hello world", }, item) ``` |
3
yaott2020 2023-03-06 12:47:57 +08:00 via Android
goland 真的很好用
|
4
awanganddong OP 谢谢大家了
|