将数据从一个 Golang 切片结构复制到另一个切片结构。

huangapple go评论120阅读模式
英文:

copy data from one golang slice struct to another slice strcut

问题

package main

import (
"encoding/json"
"fmt"
)

type Machine struct {
hostname string
Ip string
Name string
}

type Host struct {
HostName string json:"host_name"
Vars Var json:"vars"
}

type Service struct {
ServiceName string json:"service_name"
Required bool json:"required"
Hosts []Host json:"hosts"
Vars Var json:"vars"
}

func main() {
machineInfo := [{"dns":"1.1.1.1.eu-south-1.compute.amazonaws.com","ip":"1.1.1.1","name":"Machine-1"},{"dns":"1.1.1.2.eu-south-1.compute.amazonaws.com","ip":"1.1.1.2","name":"Machine-2"}]

  1. var machines []Machine
  2. var mService Service
  3. json.Unmarshal([]byte(machineInfo), &machines)
  4. for i := range machines {
  5. host := Host{
  6. HostName: machines[i].hostname,
  7. Vars: Var{}, // You need to define the Var struct
  8. }
  9. mService.Hosts = append(mService.Hosts, host)
  10. }
  11. fmt.Printf("Machines: %v", mService)
  12. data, _ := json.Marshal(mService)
  13. fmt.Println(string(data))

}

英文:

Hi I have json data which I Unmarshal tomachines slice . Now I am looking to copy/append each hostname from machines slice struct to service Struct []hosts . I have tried few methods but struggling to iterate over []hosts slice in service struct .

Just wondering what is the best way to copy the value from one slice struct to another slice value inside some other struct.

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Machine struct {
  7. hostname string
  8. Ip string
  9. Name string
  10. }
  11. type Host struct {
  12. HostName string `json:"host_name"`
  13. Vars Var `json:"vars"`
  14. }
  15. type Service struct {
  16. ServiceName string `json:"service_name"`
  17. Required bool `json:"required"`
  18. Hosts []Host `json:"hosts"`
  19. Vars Var `json:"vars"`
  20. }
  21. func main() {
  22. machineInfo := `[{"dns":"1.1.1.1.eu-south-1.compute.amazonaws.com","ip":"1.1.1.1","name":"Machine-1"},{"dns":"1.1.1.2.eu-south-1.compute.amazonaws.com","ip":"1.1.1.2","name":"Machine-2"}]`
  23. var machines []Machine
  24. var mService *Service
  25. //convert the json to byts slice and then
  26. json.Unmarshal([]byte(machineInfo), &machines)
  27. //Data is now add to the structs
  28. for i, _ := range machines {
  29. mService.Hosts = append(mService.Hosts, machines[i].hostname)
  30. }
  31. fmt.Printf("Machines : %v", mService)
  32. data, _ := json.Marshal(mService)
  33. fmt.Println(string(data))
  34. }

答案1

得分: 2

让我们从头开始:

通过将主机名字段大写来导出它。JSON解码器会忽略意外的字段。此外,添加一个标签以使该字段与文档匹配:

  1. type Machine struct {
  2. Hostname string `json:"name"`
  3. Ip string
  4. Name string
  5. }

声明mServices为一个值,以避免空指针恐慌:

  1. var mService Service

使用复合字面表达式创建一个主机,以便将其附加到切片中:

  1. mService.Hosts = append(mService.Hosts, Host{HostName: machines[i].Hostname})

https://go.dev/play/p/nTBVwM3l9Iw

英文:

Let's take it from the top:

Export the hostname field by capitalizing it. The JSON decoder ignores unexpected fields. Also, add a tag so the field matches the document:

  1. type Machine struct {
  2. Hostname string `json:"name"`
  3. Ip string
  4. Name string
  5. }

Declare mServices as a value to avoid nil pointer panic:

  1. var mService Service

Use a composite literal expression to create a host for append to the slice:

  1. mService.Hosts = append(mService.Hosts, Host{HostName: machines[i].Hostname})

https://go.dev/play/p/nTBVwM3l9Iw

huangapple
  • 本文由 发表于 2022年2月27日 11:17:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/71281845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定