英文:
golang: declaring a camelcase in a struct declaration
问题
我正在尝试通过Golang读取一个YAML文件。
但是"matchLabels"子结构没有被识别。
YAML文件如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
app: test
spec:
replicas: 3
selector:
matchLabels:
app: web
结构体如下:
type myData struct {
Apivesion string `yaml:"apiVersion"`
Kind string
Metadata struct {
Name string
Labels struct {
App string
}
}
Spec struct {
Replicas int64
Selector struct {
Matchlabels struct {
App string
}
}
}
}
期望结果:
&{apps/v1 Deployment {nginx-deploy {test}} {3 {{web}}}}
实际结果:
&{apps/v1 Deployment {nginx-deploy {test}} {3 {{}}}}
修复方法没有生效:
Matchlabels struct
yaml:"matchLabels" {
英文:
I'm trying to read a yaml file via golang.
But the "matchLabels" sub-struct is not being recognized
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
app: test
spec:
replicas: 3
selector:
matchLabels:
app: web
struct
type myData struct {
Apivesion string `yaml:"apiVersion"`
Kind string
Metadata struct {
Name string
Labels struct {
App string
}
}
Spec struct {
Replicas int64
Selector struct {
Matchlabels struct {
App string
}
}
}
}
Expectation
&{apps/v1 Deployment {nginx-deploy {test}} {3 {{web}}}}
Result
&{apps/v1 Deployment {nginx-deploy {test}} {3 {{}}}}
Fix didn't work:
Matchlabels struct `yaml:"matchLabels"` {
答案1
得分: 1
Cerise Limón 在 评论中 给出了答案:
字段标签跟在类型后面:
Matchlabels struct { App string } `yaml:"matchLabels"`
英文:
Cerise Limón gave the answer in a comment:
> Field tags follow the type:
> ~~~
> Matchlabels struct { App string }
> yaml:"matchLabels"
> ~~~
答案2
得分: 0
我不认为你正确地实现了建议的答案,请看一下标签的位置:
type myData struct {
Apivesion string `yaml:"apiVersion"`
Kind string
Metadata struct {
Name string
Labels struct {
App string
}
}
Spec struct {
Replicas int64
Selector struct {
Matchlabels struct {
App string
} `yaml:"matchLabels"`
}
}
}
参考链接:https://go.dev/play/p/yd9c-iBz2yL
英文:
I don't think you implemented the suggested answer correctly, see where the tag is:
type myData struct {
Apivesion string `yaml:"apiVersion"`
Kind string
Metadata struct {
Name string
Labels struct {
App string
}
}
Spec struct {
Replicas int64
Selector struct {
Matchlabels struct {
App string
} `yaml:"matchLabels"`
}
}
}
See also: https://go.dev/play/p/yd9c-iBz2yL
答案3
得分: 0
type AutoGenerated struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata struct {
Name string `yaml:"name"`
Labels struct {
App string `yaml:"app"`
} `yaml:"labels"`
} `yaml:"metadata"`
Spec struct {
Replicas int `yaml:"replicas"`
Selector struct {
MatchLabels struct {
App string `yaml:"app"`
} `yaml:"matchLabels"`
} `yaml:"selector"`
} `yaml:"spec"`
}
你可以使用这个工具,它非常有用:
英文:
type AutoGenerated struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata struct {
Name string `yaml:"name"`
Labels struct {
App string `yaml:"app"`
} `yaml:"labels"`
} `yaml:"metadata"`
Spec struct {
Replicas int `yaml:"replicas"`
Selector struct {
MatchLabels struct {
App string `yaml:"app"`
} `yaml:"matchLabels"`
} `yaml:"selector"`
} `yaml:"spec"`
}
You can use this tool, it is really helpful:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论