As I kind of loath the idea of creating a bunch of top level types for something used one time in one place, I am trying to create an anonymous struct nested inside an anonymous struct for a Json RPC call.
The anon struct would match the following non-anon types
type jsonRpcRequest struct {
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Id string `json:"id"`
Params params `json:"params"`
}
type params struct {
Format string `json:"format"`
Cmds []string `json:"cmds"`
Version int `json:"version"`
}
by doing the following
package main
func main() {
data := struct {
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Id string `json:"id"`
Params struct {
Format string `json:"format"`
Cmds []string `json:"cmds"`
Version int `json:"version"`
} `json:"params"`
}{
JsonRpc: "2.0",
Method: "someMethod",
Id: "someId",
Params: {
Format: "json",
Cmds: []string{"some", "list", "of", "commands"},
Version: 1,
},
}
}
However I get the following error
./anon_struct_inside_anon_struct_example.go:17:11: missing type in composite literal
Is this even possible with golang syntax?
I cannot seem to get past the fact that golang wants to separate the type and value blocks so it seems there is no way to satisfy the compiler for the inner struct.