我有这个结构:
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"`
}
我尝试初始化电路和错误,但是我不知道如何:
allCircuits := RemedyCircuitsResp{}
allCircuits.Circuits = make([]RemedyCircuitsResp.Circuits, 0)
allCircuits.ResponseStatus.Errors = make([]RemedyCircuitsResp.ResponseStatus.Errors, 0)
Visual Studio代码说:
RemedyCircuitsResp.Circuits undefined (type RemedyCircuitsResp has no method Circuits)
RemedyCircuitsResp.ResponseStatus undefined (type RemedyCircuitsResp has no method ResponseStatus)
答案 0 :(得分:6)
不幸的是,您无法定义匿名结构,然后以非匿名方式对其进行初始化。如果要这样做,则必须再次键入整个结构。这样比较容易。
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)
}