Reputation: 405
I am marshaling values into a struct from JSON. This is my struct:
type AutoGenerated struct {
ID int64 `json:"id"`
SuccessHTTPResponseCode int `json:"success_http_response_code"`
MaxRetries int `json:"max_retries"`
CallbackWebhookURL string `json:"callback_webhook_url"`
Request struct {
URL string `json:"url"` (error occurs here)
Method string `json:"method"`
HTTPHeaders struct {
ContentType string `json:"content-Type"`
Accept string `json:"accept"`
} `json:"http_headers"`
Body struct {
Foo string `json:"foo"`
} `json:"body"`
} `json:"request"`
}
Below is the function where I marshal it:
func createBSON() []byte {
data1:= AutoGenerated{
ID: 1462406556741,
SuccessHTTPResponseCode: 200,
MaxRetries: 3,
CallbackWebhookURL: "http://requestb.in/vh61ztvh",
Request: {
URL: "http://requestb.in/vh61ztvh",
Method: "POST",
HTTPHeaders: {
ContentType: "Application/json",
Accept: "Application/json",
},
Body : {
Foo: "bar",
},
},
}
sample,err:=json.Marshal(data1)
check(err)
fmt.Print(sample)
return sample
}
I made a couple of changes and the above is my updated function. I am getting the following error:
missing type in composite literal
I am kind of new to Golang. I can't figure out what this error is. Any help would be appreciated.
Upvotes: 2
Views: 1679
Reputation: 405
Okay, after referring to the links posted by Paul and PieOhPah this is how i created my struct :
type AutoGenerated struct {
ID int64 `json:"id"`
SuccessHTTPResponseCode int `json:"success_http_response_code"`
MaxRetries int `json:"max_retries"`
CallbackWebhookURL string `json:"callback_webhook_url"`
Request `json:"request"`
}
type Request struct{
URL string `json:"url"`
Method string `json:"method"`
HTTPHeaders `json:"http_headers"`
Body `json:"body"`
}
type HTTPHeaders struct{
ContentType string `json:"content-Type"`
Accept string `json:"accept"`
}
type Body struct{
Foo string `json:"foo"`
}
Here is the function in which I initialize and Marshal it:
func createBSON() []byte {
data1:= AutoGenerated{
ID: 1462406556741,
SuccessHTTPResponseCode: 200,
MaxRetries: 3,
CallbackWebhookURL: "http://requestb.in/vh61ztvh",
Request: Request{
URL: "http://requestb.in/vh61ztvh",
Method: "POST",
HTTPHeaders: HTTPHeaders {
ContentType: "Application/json",
Accept: "Application/json",
},
Body : Body {
Foo: "bar",
},
},
}
fmt.Print(data1)
sample,err:=json.Marshal(data1)
s := string(sample)
fmt.Println(s)
return sample
}
I posted this hoping this it will be useful for others who encounter deeply nested structs in Go.
Upvotes: 0
Reputation: 8390
When you use an anonymous struct like this:
type AutoGenerate struct {
Request: struct {
URL string
Method string
}
}
This whole chunk is the type name
struct {
URL string
Method string
}
In another word, you'll have to initiate this way
data := AutoGenerate{
Request: struct {
URL string
Method string
}{
URL: "http://somedomain.com/",
Method: "GET",
},
}
Thus, in your case, it is better to separate each struct into a named one:
type Request struct {
URL string
Method string
}
type AutoGenerate struct {
Request Request
}
Please see https://play.golang.org/p/kZDN2yhlkz of the chaos it will become with anonymous structs.
Upvotes: 3