How to write getter and setter for a nested struct

i’m trying to write a method for getting and setting values to struct ,
This is the struct i’m writing getter and setter for

type Deployments struct {

Namespace      string            `json:"namespace"`
Replicas       int               `json:"replicas"`
Image          string            `json:"images"`
LivenessProbe  LivenessProbes    `json:"livenessprobe"`
Port           []DeploymentPorts `json:"port"`
ReadinessProbe ReadinessProbes   `json:"readinessprobe"`
Limit          Limits            `json:"limit"`
Request        Requests          `json:"request"`
VolumeMount    []VolumeMounts    `json:"volumemount"`
NodeSelectorKey   string `json:"nodeselectorkey"`
NodeSelectorValue string `json:"nodeselectorvalue"`

}

here’s my setter func

 //SetDeployment func
func (d *Deployments) SetDeployment (Namespace string, Replicas int, Image string, LivenessProbe LivenessProbes, port DeploymentPorts, ReadinessProbe ReadinessProbes, Limit Limits, Request Requests, VolumeMount VolumeMounts, NodeSelectorKey string, NodeSelectorValue string) 
{

d.Namespace = Namespace
d.Replicas = Replicas
d.Image = Image
d.LivenessProbe = LivenessProbe
d.Port = append(d.Port, port)
d.ReadinessProbe = ReadinessProbe
d.Limit = Limit
d.Request = Request
d.VolumeMount = append(d.VolumeMount, VolumeMount)
d.NodeSelectorKey = NodeSelectorKey
d.NodeSelectorValue = NodeSelectorValue
}

but when i call setter to add a value

        deployment := models.NewDeployment()

		value := deployment.SetDeployment("verizon", 3, "image", models.LivenessProbes{LivenessFailureThreshold: 3, HTTPGetPath: "path", HTTPGetPort: 80, HTTPGetScheme: "http", LivenessInitialDelay: 12, LivenessPeriodSeconds: 10, LivenessSuccessThreshold: 2, LivenessTimeout: 12},	 models.DeploymentPorts{ContainerPort: 80, Protocol: "TCP"}, models.ReadinessProbes{HTTPGetPath: "path", HTTPGetPort: 80, HTTPGetScheme: "http", ReadinessFailureThreshold: 10, ReadinessInitialDelay: 10, ReadinessPeriodSeconds: 10, ReadinessSuccessThreshold: 10, ReadinessTimeout: 10}, models.Limits{LimitCPU: "4gb", LimitMemory: "8gb"}, models.Requests{RequestCPU: "4gb", RequestMemory: "8gb"}, models.VolumeMounts{MountName: "name", MountPath: "/"}, "work", "nfs")

i get an error saying used as value

2 Likes

That method changes its receiver as a side effect and does not return anything. You can not assign it.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.