dongyan7172 2019-05-06 15:31
浏览 102
已采纳

如何在GO中初始化结构的嵌套数组?

I'm having this struct:

type RemedyCircuitsResp struct {
    Circuits []struct {
        RemedyCircuitID            string `json:"RemedyCircuitId"`
        Status                     string `json:"Status"`
        VendorName                 string `json:"VendorName"`
        VendorCommunityID          int    `json:"VendorCommunityId"`
        CommunityID                int    `json:"CommunityId"`
        ZLocCommunityID            int    `json:"ZLocCommunityId"`
        CircuitType                string `json:"CircuitType"`
        InstalledSpeed             string `json:"InstalledSpeed"`
        CircuitID                  string `json:"CircuitId"`
        CircuitSpeed               string `json:"CircuitSpeed"`
        CircuitFunction            string `json:"CircuitFunction"`
        ContractID                 string `json:"ContractId"`
        ALocName                   string `json:"ALocName"`
        ZLocName                   string `json:"ZLocName"`
        ExpectedMonthlyCircuitCost string `json:"ExpectedMonthlyCircuitCost"`
        ExpectedOneTimeCircuitCost string `json:"ExpectedOneTimeCircuitCost"`
        TotalCost                  string `json:"TotalCost"`
        CustomerMakeReadyComplete  string `json:"CustomerMakeReadyComplete"`
        CarrierOriginalDueDate     string `json:"CarrierOriginalDueDate"`
        CarrierCurrentDueDate      string `json:"CarrierCurrentDueDate"`
        CarrierCompletion          string `json:"CarrierCompletion"`
        EnaTurnUpDate              string `json:"EnaTurnUpDate"`
        SiteVisit                  string `json:"SiteVisit"`
        DisconnectRequested        string `json:"DisconnectRequested"`
        DisconnectEffective        string `json:"DisconnectEffective"`
    } `json:"Circuits"`
    ResponseStatus struct {
        ErrorCode  string `json:"ErrorCode"`
        Message    string `json:"Message"`
        StackTrace string `json:"StackTrace"`
        Errors     []struct {
            ErrorCode string `json:"ErrorCode"`
            FieldName string `json:"FieldName"`
            Message   string `json:"Message"`
        } `json:"Errors"`
    } `json:"ResponseStatus"`
}

I've tried to initialize Circuits and Errors but I don't know how:

    allCircuits := RemedyCircuitsResp{}
    allCircuits.Circuits = make([]RemedyCircuitsResp.Circuits, 0)
    allCircuits.ResponseStatus.Errors = make([]RemedyCircuitsResp.ResponseStatus.Errors, 0)

Visual Studio Code says:

RemedyCircuitsResp.Circuits undefined (type RemedyCircuitsResp has no method Circuits)
RemedyCircuitsResp.ResponseStatus undefined (type RemedyCircuitsResp has no method ResponseStatus)
  • 写回答

1条回答 默认 最新

  • douchongzhang9267 2019-05-06 15:39
    关注

    Unfortunately you cannot define an anonymous struct and then initialize it un-anonymously. If you want to do that, you have to type out the entire struct all over again. This way is easier.

    package main
    
    import (
        "fmt"
    )
    
    type Circuit struct {
        RemedyCircuitID            string `json:"RemedyCircuitId"`
        Status                     string `json:"Status"`
        VendorName                 string `json:"VendorName"`
        VendorCommunityID          int    `json:"VendorCommunityId"`
        CommunityID                int    `json:"CommunityId"`
        ZLocCommunityID            int    `json:"ZLocCommunityId"`
        CircuitType                string `json:"CircuitType"`
        InstalledSpeed             string `json:"InstalledSpeed"`
        CircuitID                  string `json:"CircuitId"`
        CircuitSpeed               string `json:"CircuitSpeed"`
        CircuitFunction            string `json:"CircuitFunction"`
        ContractID                 string `json:"ContractId"`
        ALocName                   string `json:"ALocName"`
        ZLocName                   string `json:"ZLocName"`
        ExpectedMonthlyCircuitCost string `json:"ExpectedMonthlyCircuitCost"`
        ExpectedOneTimeCircuitCost string `json:"ExpectedOneTimeCircuitCost"`
        TotalCost                  string `json:"TotalCost"`
        CustomerMakeReadyComplete  string `json:"CustomerMakeReadyComplete"`
        CarrierOriginalDueDate     string `json:"CarrierOriginalDueDate"`
        CarrierCurrentDueDate      string `json:"CarrierCurrentDueDate"`
        CarrierCompletion          string `json:"CarrierCompletion"`
        EnaTurnUpDate              string `json:"EnaTurnUpDate"`
        SiteVisit                  string `json:"SiteVisit"`
        DisconnectRequested        string `json:"DisconnectRequested"`
        DisconnectEffective        string `json:"DisconnectEffective"`
    }
    
    type Error struct {
        ErrorCode string `json:"ErrorCode"`
        FieldName string `json:"FieldName"`
        Message   string `json:"Message"`
    }
    
    type ResponseStatus struct {
        ErrorCode  string  `json:"ErrorCode"`
        Message    string  `json:"Message"`
        StackTrace string  `json:"StackTrace"`
        Errors     []Error `json:"Errors"`
    }
    
    type RemedyCircuitsResp struct {
        Circuits       []Circuit      `json:"Circuits"`
        ResponseStatus ResponseStatus `json:"ResponseStatus"`
    }
    
    func main() {
        fmt.Printf("%#v
    ", RemedyCircuitsResp{
            Circuits: []Circuit{{}, {}, {}, {}},
        })
    }
    

    Alternatively you have to do something like this:

    package main
    
    import (
        "fmt"
    )
    
    type RemedyCircuitsResp struct {
        Circuits []struct {
            RemedyCircuitID            string `json:"RemedyCircuitId"`
            Status                     string `json:"Status"`
            VendorName                 string `json:"VendorName"`
            VendorCommunityID          int    `json:"VendorCommunityId"`
            CommunityID                int    `json:"CommunityId"`
            ZLocCommunityID            int    `json:"ZLocCommunityId"`
            CircuitType                string `json:"CircuitType"`
            InstalledSpeed             string `json:"InstalledSpeed"`
            CircuitID                  string `json:"CircuitId"`
            CircuitSpeed               string `json:"CircuitSpeed"`
            CircuitFunction            string `json:"CircuitFunction"`
            ContractID                 string `json:"ContractId"`
            ALocName                   string `json:"ALocName"`
            ZLocName                   string `json:"ZLocName"`
            ExpectedMonthlyCircuitCost string `json:"ExpectedMonthlyCircuitCost"`
            ExpectedOneTimeCircuitCost string `json:"ExpectedOneTimeCircuitCost"`
            TotalCost                  string `json:"TotalCost"`
            CustomerMakeReadyComplete  string `json:"CustomerMakeReadyComplete"`
            CarrierOriginalDueDate     string `json:"CarrierOriginalDueDate"`
            CarrierCurrentDueDate      string `json:"CarrierCurrentDueDate"`
            CarrierCompletion          string `json:"CarrierCompletion"`
            EnaTurnUpDate              string `json:"EnaTurnUpDate"`
            SiteVisit                  string `json:"SiteVisit"`
            DisconnectRequested        string `json:"DisconnectRequested"`
            DisconnectEffective        string `json:"DisconnectEffective"`
        } `json:"Circuits"`
        ResponseStatus struct {
            ErrorCode  string `json:"ErrorCode"`
            Message    string `json:"Message"`
            StackTrace string `json:"StackTrace"`
            Errors     []struct {
                ErrorCode string `json:"ErrorCode"`
                FieldName string `json:"FieldName"`
                Message   string `json:"Message"`
            } `json:"Errors"`
        } `json:"ResponseStatus"`
    }
    
    func main() {
        value := RemedyCircuitsResp{
            Circuits: []struct {
                RemedyCircuitID            string `json:"RemedyCircuitId"`
                Status                     string `json:"Status"`
                VendorName                 string `json:"VendorName"`
                VendorCommunityID          int    `json:"VendorCommunityId"`
                CommunityID                int    `json:"CommunityId"`
                ZLocCommunityID            int    `json:"ZLocCommunityId"`
                CircuitType                string `json:"CircuitType"`
                InstalledSpeed             string `json:"InstalledSpeed"`
                CircuitID                  string `json:"CircuitId"`
                CircuitSpeed               string `json:"CircuitSpeed"`
                CircuitFunction            string `json:"CircuitFunction"`
                ContractID                 string `json:"ContractId"`
                ALocName                   string `json:"ALocName"`
                ZLocName                   string `json:"ZLocName"`
                ExpectedMonthlyCircuitCost string `json:"ExpectedMonthlyCircuitCost"`
                ExpectedOneTimeCircuitCost string `json:"ExpectedOneTimeCircuitCost"`
                TotalCost                  string `json:"TotalCost"`
                CustomerMakeReadyComplete  string `json:"CustomerMakeReadyComplete"`
                CarrierOriginalDueDate     string `json:"CarrierOriginalDueDate"`
                CarrierCurrentDueDate      string `json:"CarrierCurrentDueDate"`
                CarrierCompletion          string `json:"CarrierCompletion"`
                EnaTurnUpDate              string `json:"EnaTurnUpDate"`
                SiteVisit                  string `json:"SiteVisit"`
                DisconnectRequested        string `json:"DisconnectRequested"`
                DisconnectEffective        string `json:"DisconnectEffective"`
            }{{}, {}, {}, {}},
        }
        fmt.Printf("%#v
    ", value)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证