英文:
go Lang load unable to process yaml file
问题
我正在尝试读取一个 YAML 文件并将其存储在一个变量中,但由于某种原因,YAML 文件中的数组对象无法解组该文件,显示为空数据。
以下是我的 YAML 文件内容:
---
version: "1.2"
bUnits:
  - name: buname
    buGroupID: asd
    buStGroupID: asd
  - name: buname2
    buGroupID: asd2
    buStGroupID: asd2
以下是我使用的代码:
type SResponse struct {
   Version string `json:"version"`
   BUnits  []BUnit `json:"bUnits"`
}
type BUnit struct {
    Name            string `json:"name"`
    BuUnitGroupID   string `json:"buGroupID"`
    BuUnitStGroupID string `json:"buStaticGroupID"`
}
func main() {
    _printf := fmt.Printf
    _printf("Start")
    var sListResponse SResponse
    source, err2 := ioutil.ReadFile("squads2.yml")
    if err2 != nil {
        _printf("Couldn't read yaml file.")
    }
    err2 = yaml.Unmarshal(source, &sListResponse)
    if err2 != nil {
        _printf("Error")
    }
    _printf("Output: %s\n", sListResponse)
}
代码可以读取版本部分,但是 bUnits 数组为空。请提供建议。
英文:
I am trying to read a yaml file and store it in a variable but for some reason the array object in YAML file is unable to unmarshall the file. it shows blank data.
Below is my yaml file content
---
version: "1.2"
bUnits:
  - name: buname
    buGroupID: asd
    buStGroupID: asd
  - name: buname2
    buGroupID: asd2
    buStGroupID: asd2
Below is the code that am using
type SResponse struct {
   Version       string         `json:"version"`
   BUnits []BUnit `json:"bUnits"`
}
type BUnit struct {
	Name                      string `json:"name"`
    BuUnitGroupID       string `json:"buGroupID"`
    BuUnitStGroupID string `json:"buStaticGroupID"`
}
func main() {
    _printf := fmt.Printf
    _printf("Start")
    var sListResponse SResponse
    source, err2 := ioutil.ReadFile("squads2.yml")
    if err2 != nil {
    	_printf("Couldn't read yaml file.")
}
    err2 = yaml.Unmarshal(source, &sListResponse)
    if err2 != nil {
	_printf("Error")
    }
    _printf("Output: %s\n", sListResponse)
}
The code reads version part but the bUnits array comes empty.Please suggest.
答案1
得分: 0
你的结构体使用了json标签,这可能是因为你之后需要将数据导出/导入为JSON格式。但是你目前遇到的问题是yaml导入,所以你需要在结构体定义中添加yaml标签。
为了同时支持json和yaml的编组/解组,你可以简单地更新你的标签,像这样:
type SResponse struct {
    Version string  `json:"version" yaml:"version"`
    BUnits  []BUnit `json:"bUnits" yaml:"bUnits"`
}
type BUnit struct {
    Name            string `json:"name" yaml:"name"`
    BuUnitGroupID   string `json:"buGroupID" yaml:"buGroupID"`
    BuUnitStGroupID string `json:"buStaticGroupID" yaml:"buStaticGroupID"`
}
如果你不需要进行JSON编码/解码,也可以直接删除json标签。
英文:
Your structs are decorated with json tags - which you may or may not need (depending on if you are exporting/import this data in JSON later).
But your problem at hand is with yaml importing - so you need to decorate your struct definitions with yaml tags.
To support both json and yaml marshaling/unmarshaling, simply update your tags like so:
type SResponse struct {
	Version string  `json:"version" yaml:"version"`
	BUnits  []BUnit `json:"bUnits" yaml:"bUnits"`
}
type BUnit struct {
	Name            string `json:"name" yaml:"name"`
	BuUnitGroupID   string `json:"buGroupID" yaml:"buGroupID"`
	BuUnitStGroupID string `json:"buStaticGroupID" yaml:"buStaticGroupID"`
}
Or just drop the json tags if you have no need for JSON encoding/decoding.
答案2
得分: 0
你应该在你的结构体中使用yaml标签。
type SResponse struct {
	Version string  `yaml:"version"`
	BUnits  []BUnit `yaml:"bUnits"`
}
type BUnit struct {
	Name            string `yaml:"name"`
	BuUnitGroupID   string `yaml:"buGroupID"`
	BuUnitStGroupID string `yaml:"buStaticGroupID"`
}
这样,你的结构体字段就可以正确地与 YAML 数据进行映射了。
英文:
you should use yaml tag in your struct
type SResponse struct {
	Version string  `yaml:"version"`
	BUnits  []BUnit `yaml:"bUnits"`
}
type BUnit struct {
	Name            string `yaml:"name"`
	BuUnitGroupID   string `yaml:"buGroupID"`
	BuUnitStGroupID string `yaml:"buStaticGroupID"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论