简体   繁体   English

如何在 Golang 中为嵌套数组结构初始化变量?

[英]How initialize a variable for nested array struct in Golang?

I am parsing a JSON file in Golang, by making a nested struct, and able to do it successfully.我正在 Golang 中解析一个 JSON 文件,通过创建一个嵌套的结构,并且能够成功地做到这一点。 However, now I wish to make a variable of the same struct, but I get the following error cannot use []Specs literal (type []Specs) as type []Specs in field value .但是,现在我希望创建一个具有相同结构的变量,但出现以下错误cannot use []Specs literal (type []Specs) as type []Specs in field value Could someone please point out my mistake here?有人可以在这里指出我的错误吗? What am I doing wrong?我究竟做错了什么?

This is the Nested Struct:这是嵌套结构:

type Config struct {
OrdererOrgs []OrdererOrgs `json:"OrdererOrgs"`
PeerOrgs []PeerOrgs `json:"PeerOrgs"`
}

type OrdererOrgs struct {
Name string `json:"name"`
Domain string `json:"Domain"`
Specs []Specs `json:"Specs"`
}

type Specs struct {
Hostname string `json:"Hostname"`
Commonname string `json:"Commonname"`
}

type PeerOrgs struct {
Name   string `json:"name"`
Domain   string `json:"Domain"`
Template Template `json:"Template"`
Users Users `json:"Users"`
}

type Template struct {
Count int `json:"Count"`
Start int `json:"Start"`
}

type Users struct {
Count int `json:"Count"`
}

And this is my variable:这是我的变量:

newconfig:= Config{
    OrdererOrgs: []OrdererOrgs{
        OrdererOrgs{
            Name: "Orderer1",
            Domain : "Domain",
            Specs: []Specs{
                Specs{
                Hostname: "H",
                Commonname: "C",
                },
                Specs{
                    Hostname: "H",
                    Commonname: "C",
                    },
            },
        },
        OrdererOrgs{
            Name: "Orderer2",
            Domain : "Domain2",
            Specs: []Specs{
                Specs{
                Hostname: "H",
                Commonname: "C",
                },
            },
        },
    },
    PeerOrgs: []PeerOrgs{
        PeerOrgs{
            Name: "Org1",
            Domain: "D",
            Template: Template{
                Count: 1,
                Start: 0,
            },
            Users: Users{
                Count: 1,
            },
        },
        PeerOrgs{
            Name: "Org2",
            Domain: "D2",
            Template: Template{
                Count: 1,
                Start: 0,
            },
            Users: Users{
                Count: 1,
            },
        },
    },
}

Welcome to StackOverflow!欢迎使用 StackOverflow! As Volker said in a comment, your code seems to be working.正如沃尔克在评论中所说,您的代码似乎正在运行。 Are you sure you're seeing the issue now?你确定你现在看到了这个问题吗? Can you run the playground link Volker provided?你能运行 Volker 提供的游乐场链接吗?

In general, this is possible in Go via the composite literals feature ;通常,这可以通过复合文字功能在 Go 中实现; that link has several useful examples along with a discussion of semantics that you may find interesting.该链接有几个有用的示例以及您可能会感兴趣的语义讨论。 If something stops working I'd recommend building progressively more complicate structures until you see where the problem is, until you reach the level of nesting needed for your application.如果某些东西停止工作,我建议逐步构建更复杂的结构,直到您看到问题所在,直到达到应用程序所需的嵌套级别。

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

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