gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:22:50 26 4
gpt4 key购买 nike

我有这个结构:

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"`
}

我已经尝试初始化 Circuits 和 Errors,但我不知道如何:

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

Visual Studio Code 说:

RemedyCircuitsResp.Circuits undefined (type RemedyCircuitsResp has no method Circuits)
RemedyCircuitsResp.ResponseStatus undefined (type RemedyCircuitsResp has no method ResponseStatus)

最佳答案

不幸的是,您不能定义一个匿名结构,然后以非匿名方式对其进行初始化。如果你想这样做,你必须重新输入整个结构。这种方式更简单。

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\n", RemedyCircuitsResp{
Circuits: []Circuit{{}, {}, {}, {}},
})
}

或者你必须做这样的事情:

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\n", value)
}

关于go - 如何在 GO 中初始化嵌套结构数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56008287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com