golang: declaring a camelcase in a struct declaration

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

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

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

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

英文:
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:

> 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:

确定