douyan8266 2019-01-15 13:26
浏览 147

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

I am parsing a JSON file in Golang, by making a nested struct, and able to do it successfully. 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. 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,
            },
        },
    },
}
  • 写回答

1条回答 默认 最新

  • douhuang2282 2019-01-15 13:49
    关注

    Welcome to 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?

    In general, this is possible in Go via the composite literals feature; 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?