像这种结构怎么给赋值

2023-03-06 12:10:42 +08:00
 awanganddong
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))
}
1272 次点击
所在节点    Go 编程语言
4 条回复
rrfeng
2023-03-06 12:16:57 +08:00
ToUser: struct {
ID int `json:"id"`
}{
ID: 10,
}
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)
```
yaott2020
2023-03-06 12:47:57 +08:00
goland 真的很好用
awanganddong
2023-03-06 14:01:27 +08:00
谢谢大家了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/921533

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX