golang: declaring a camelcase in a struct declaration

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

golang: declaring a camelcase in a struct declaration

问题

我正在尝试通过Golang读取一个YAML文件。

但是"matchLabels"子结构没有被识别。

YAML文件如下:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deploy
  5. labels:
  6. app: test
  7. spec:
  8. replicas: 3
  9. selector:
  10. matchLabels:
  11. app: web

结构体如下:

  1. type myData struct {
  2. Apivesion string `yaml:"apiVersion"`
  3. Kind string
  4. Metadata struct {
  5. Name string
  6. Labels struct {
  7. App string
  8. }
  9. }
  10. Spec struct {
  11. Replicas int64
  12. Selector struct {
  13. Matchlabels struct {
  14. App string
  15. }
  16. }
  17. }
  18. }

期望结果:

&{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

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deploy
  5. labels:
  6. app: test
  7. spec:
  8. replicas: 3
  9. selector:
  10. matchLabels:
  11. app: web

struct

  1. type myData struct {
  2. Apivesion string `yaml:"apiVersion"`
  3. Kind string
  4. Metadata struct {
  5. Name string
  6. Labels struct {
  7. App string
  8. }
  9. }
  10. Spec struct {
  11. Replicas int64
  12. Selector struct {
  13. Matchlabels struct {
  14. App string
  15. }
  16. }
  17. }
  18. }

Expectation

  1. &{apps/v1 Deployment {nginx-deploy {test}} {3 {{web}}}}

Result

  1. &{apps/v1 Deployment {nginx-deploy {test}} {3 {{}}}}

Fix didn't work:

  1. Matchlabels struct `yaml:"matchLabels"` {

答案1

得分: 1

Cerise Limón评论中 给出了答案:

字段标签跟在类型后面:

  1. Matchlabels struct { App string }
  2. `yaml:"matchLabels"`
英文:

Cerise Limón gave the answer in a comment:

> Field tags follow the type:
> ~~~
> Matchlabels struct { App string }
> yaml:"matchLabels"
> ~~~

答案2

得分: 0

我不认为你正确地实现了建议的答案,请看一下标签的位置:

  1. type myData struct {
  2. Apivesion string `yaml:"apiVersion"`
  3. Kind string
  4. Metadata struct {
  5. Name string
  6. Labels struct {
  7. App string
  8. }
  9. }
  10. Spec struct {
  11. Replicas int64
  12. Selector struct {
  13. Matchlabels struct {
  14. App string
  15. } `yaml:"matchLabels"`
  16. }
  17. }
  18. }

参考链接:https://go.dev/play/p/yd9c-iBz2yL

英文:

I don't think you implemented the suggested answer correctly, see where the tag is:

  1. type myData struct {
  2. Apivesion string `yaml:"apiVersion"`
  3. Kind string
  4. Metadata struct {
  5. Name string
  6. Labels struct {
  7. App string
  8. }
  9. }
  10. Spec struct {
  11. Replicas int64
  12. Selector struct {
  13. Matchlabels struct {
  14. App string
  15. } `yaml:"matchLabels"`
  16. }
  17. }
  18. }

See also: https://go.dev/play/p/yd9c-iBz2yL

答案3

得分: 0

  1. type AutoGenerated struct {
  2. APIVersion string `yaml:"apiVersion"`
  3. Kind string `yaml:"kind"`
  4. Metadata struct {
  5. Name string `yaml:"name"`
  6. Labels struct {
  7. App string `yaml:"app"`
  8. } `yaml:"labels"`
  9. } `yaml:"metadata"`
  10. Spec struct {
  11. Replicas int `yaml:"replicas"`
  12. Selector struct {
  13. MatchLabels struct {
  14. App string `yaml:"app"`
  15. } `yaml:"matchLabels"`
  16. } `yaml:"selector"`
  17. } `yaml:"spec"`
  18. }

你可以使用这个工具,它非常有用:

https://zhwt.github.io/yaml-to-go/

英文:
  1. type AutoGenerated struct {
  2. APIVersion string `yaml:"apiVersion"`
  3. Kind string `yaml:"kind"`
  4. Metadata struct {
  5. Name string `yaml:"name"`
  6. Labels struct {
  7. App string `yaml:"app"`
  8. } `yaml:"labels"`
  9. } `yaml:"metadata"`
  10. Spec struct {
  11. Replicas int `yaml:"replicas"`
  12. Selector struct {
  13. MatchLabels struct {
  14. App string `yaml:"app"`
  15. } `yaml:"matchLabels"`
  16. } `yaml:"selector"`
  17. } `yaml:"spec"`
  18. }

You can use this tool, it is really helpful:

> https://zhwt.github.io/yaml-to-go/

huangapple
  • 本文由 发表于 2022年8月26日 08:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/73494857.html
匿名

发表评论

匿名网友

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

确定