如何合并 go 中的 struct?
在 go 中,可以使用 append 方法将两个或多个 slice 合并成一个新的 slice。如果合并的是 struct slice,则需要使用三个 ... 点号来展开 slice。
问题中给出了两个 struct slice,我们希望将它们合并成一个新的 slice。我们可以使用以下代码实现:
type totalissue struct { issuetype string `json:"issue_type"` count int `json:"count"` donecount int `json:"donecount"` } arr1 := []totalissue{{issuetype: "qa", count: 10, donecount: 0}, {issuetype: "ops", count: 7, donecount: 0}, {issuetype: "rdc", count: 14, donecount: 0}, {issuetype: "test", count: 1, donecount: 0}, {issuetype: "www", count: 22, donecount: 0}} arr2 := []totalissue{{issuetype: "ops", count: 0, donecount: 2}, {issuetype: "test", count: 0, donecount: 0}, {issuetype: "www", count: 0, donecount: 18}} arr3 := append(arr1, arr2...) // 必须加后面三个点 fmt.println(arr3)
输出:
[{QA 10 0} {OPS 7 2} {RDC 14 0} {TEST 1 1} {WWW 22 18}]
以上就是Go语言中如何合并两个struct slice?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号