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

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

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"}]

var machines []Machine
var mService Service

json.Unmarshal([]byte(machineInfo), &machines)

for i := range machines {
	host := Host{
		HostName: machines[i].hostname,
		Vars:     Var{}, // You need to define the Var struct
	}
	mService.Hosts = append(mService.Hosts, host)
}

fmt.Printf("Machines: %v", mService)

data, _ := json.Marshal(mService)

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.

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"}]`

	var machines []Machine
	var mService *Service
	//convert the json to byts slice and then

	json.Unmarshal([]byte(machineInfo), &machines)

	//Data is now add to the structs



	for i, _ := range machines {
		mService.Hosts = append(mService.Hosts, machines[i].hostname)
	}
	fmt.Printf("Machines : %v", mService)
	
	data, _ := json.Marshal(mService)


	fmt.Println(string(data))
}

答案1

得分: 2

让我们从头开始:

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

type Machine struct {
    Hostname string `json:"name"`
    Ip       string
    Name     string
}

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

var mService Service

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

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:

type Machine struct {
	Hostname string `json:"name"`
	Ip       string
	Name     string
}

Declare mServices as a value to avoid nil pointer panic:

var mService Service

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

	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:

确定