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

如何在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)
    }
    
    点赞 评论

相关推荐 更多相似问题