简体   繁体   English

golang中的动态嵌套结构

[英]Dynamic nested struct in golang

I want to create a dynamic struct.我想创建一个动态结构。 I am using some commands to get some information in JSON format and want to unmarshal it into a struct.我正在使用一些命令以 JSON 格式获取一些信息,并希望将其解组为一个结构。 Json look like this: Json 看起来像这样:

{
"blockdevices": [
    {
        "disk_name": "sda",
        "mountpoint": null,
        "size": "50G",
        "fstype": "mpath_member",
        "partitions": [
            {
                "disk_name": "sda1",
                "mountpoint": null,
                "size": "20G",
                "fstype": "vfat"
            },
            {
                "name": "3600a09803830566e615d5171774a3837",
                "mountpoint": null,
                "size": "50G",
                "fstype": null,
                "partitions": [
                    {
                        "disk_name": "3600a09803830566e615d5171774a3837-part1",
                        "mountpoint": "/myData",
                        "size": "20G",
                        "fstype": "vfat",
                        "partitions": [
                            {
                                "disk_name": "3600a09803830566e615d5171774a3837-part2",
                                "mountpoint": "/myData2",
                                "size": "10G",
                                "fstype": "vfat"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]}

The issue is that there may b unknown sub partitions can be 1 or can be up to any number.问题是可能有 b 个未知子分区可以是 1 也可以是任意数量。 I have created the following struct:我创建了以下结构:

Blockdevices []struct {
    DiskName   string      `json:"disk_name"`
    Mountpoint interface{} `json:"mountpoint"`
    Size       string      `json:"size"`
    Fstype     string      `json:"fstype"`
    Partitions []struct {
        DiskName      string      `json:"disk_name"`
        Mountpoint    interface{} `json:"mountpoint"`
        Size          string      `json:"size"`
        Fstype        string      `json:"fstype"`
        SubPartitions bool        `json:"sub_partitions"`
        Partitions    []struct {
            DiskName   string `json:"disk_name"`
            Mountpoint string `json:"mountpoint"`
            Size       string `json:"size"`
            Fstype     string `json:"fstype"`
            Partitions []struct {
                DiskName   string `json:"disk_name"`
                Mountpoint string `json:"mountpoint"`
                Size       string `json:"size"`
                Fstype     string `json:"fstype"`
            } `json:"partitions,omitempty"`
        } `json:"partitions,omitempty"`
    } `json:"partitions,omitempty"`
} `json:"blockdevices"`}

Its working fine for upto two sub partitions but i want a solution that can work up to no matter how many sub partitions we have.它最多可用于两个子分区,但我想要一个无论我们有多少子分区都可以工作的解决方案。 Is there any way to do so.有没有办法这样做。 The partition struct inside disk struct is same can we some how like write once but it works as loop?磁盘结构内的分区结构是一样的,我们可以像写一次但它作为循环工作吗?

Thanks is advance!谢谢是提前!

Define a Partition struct:定义一个Partition结构:

type Partition struct {
   DiskName      string      `json:"disk_name"`
   Mountpoint    interface{} `json:"mountpoint"`
   Size          string      `json:"size"`
   Fstype        string      `json:"fstype"`
   SubPartitions bool        `json:"sub_partitions"`
   Partitions    []Partition `json:"partitions"`
}

This can nest as much as you need using the partitions slice.这可以使用分区切片尽可能多地嵌套。 Use this type inside BlockDevice .BlockDevice中使用这种类型。

Structs can be defined recursively .结构可以递归定义。 Define a separate struct for Partition (as a welcome side effect, this also makes your code easier to handle, instead of defining your entire JSON structure in one big nested type) and have that struct reference itself (note the Partitions attribute in the Partition type):Partition定义一个单独的结构(作为一个受欢迎的副作用,这也使您的代码更容易处理,而不是在一个大的嵌套类型中定义整个 JSON 结构)并拥有该结构引用本身(注意Partition类型中的Partitions属性):

type Blockdevice struct {
    DiskName   string      `json:"disk_name"`
    Mountpoint interface{} `json:"mountpoint"`
    Size       string      `json:"size"`
    Fstype     string      `json:"fstype"`
    Partitions []Partition `json:"partitions"`
}

type Partition struct {
    DiskName      string      `json:"disk_name"`
    Mountpoint    interface{} `json:"mountpoint"`
    Size          string      `json:"size"`
    Fstype        string      `json:"fstype"`
    SubPartitions bool        `json:"sub_partitions"`
    Partitions    []Partition `json:"partitions"`
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM