Golang中如何为嵌套结构体编写getter和setter方法
Golang中如何为嵌套结构体编写getter和setter方法 我正在尝试为结构体编写获取和设置值的方法。
这是我正在编写getter和setter的结构体
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"`
}
这是我的setter函数
//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
}
但是当我调用setter添加值时
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")
我收到一个错误提示被用作值

更多关于Golang中如何为嵌套结构体编写getter和setter方法的实战教程也可以访问 https://www.itying.com/category-94-b0.html
2 回复
该方法会改变其接收器并产生副作用,且不返回任何内容。您无法对其进行赋值。
更多关于Golang中如何为嵌套结构体编写getter和setter方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
在Go语言中,setter方法通常不返回值,而你的代码尝试将SetDeployment方法的返回值赋给变量value,这导致了"被用作值"的错误。setter方法应该设计为不返回任何值,或者如果需要返回状态或错误,可以返回一个错误类型。
以下是修正后的setter方法示例,移除了返回值:
// SetDeployment 方法设置Deployments结构体的字段
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
}
调用setter时,直接使用方法而不赋值:
deployment := models.NewDeployment()
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")
对于getter方法,通常返回字段的值。例如,为Namespace字段编写getter:
// GetNamespace 方法返回Deployments结构体的Namespace字段
func (d *Deployments) GetNamespace() string {
return d.Namespace
}
调用getter时,可以赋值给变量:
namespace := deployment.GetNamespace()
如果你的setter需要处理错误(例如验证输入),可以修改为返回错误:
// SetDeployment 方法设置Deployments结构体的字段,并返回错误
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) error {
// 添加验证逻辑,例如检查Replicas是否为正数
if Replicas < 0 {
return errors.New("replicas must be non-negative")
}
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
return nil
}
调用时处理错误:
err := 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")
if err != nil {
// 处理错误
log.Fatal(err)
}
确保在调用setter方法时,根据方法签名正确处理返回值。

