在Go语言中,是不支持嵌套的匿名结构体的。

huangapple go评论90阅读模式
英文:

Are nested anonymous structs possible in golang?

问题

由于我是一个文本模型,无法直接运行代码。但是我可以帮助你理解代码并提供一些指导。

根据你提供的代码,你想要创建一个匿名结构体嵌套在另一个匿名结构体中,以用于一个 Json RPC 调用。然而,你遇到了一个错误,提示缺少复合字面量中的类型。

根据你的代码,你可以尝试将内部结构体的类型定义提取到顶层,然后在复合字面量中使用该类型。这样可以解决缺少类型的问题。以下是修改后的代码示例:

package main

type params struct {
    Format  string   `json:"format"`
    Cmds    []string `json:"cmds"`
    Version int      `json:"version"`
}

func main() {
    data := struct {
        JsonRpc string `json:"jsonrpc"`
        Method  string `json:"method"`
        Id      string `json:"id"`
        Params  params `json:"params"`
    }{
        JsonRpc: "2.0",
        Method:  "someMethod",
        Id:      "someId",
        Params: params{
            Format:  "json",
            Cmds:    []string{"some", "list", "of", "commands"},
            Version: 1,
        },
    }
}

请注意,我只是根据你提供的信息进行了修改,并不能保证这段代码能够完全运行。你可能还需要根据你的实际需求进行进一步的调整和修改。

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

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.

答案1

得分: 2

要做到这一点,你需要在字面量中再次告诉编译器 Params 的类型:

	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: struct {
			Format  string   `json:"format"`
			Cmds    []string `json:"cmds"`
			Version int      `json:"version"`
		}{
			Format:  "json",
			Cmds:    []string{"some", "list", "of", "commands"},
			Version: 1,
		},
	}

正如你所看到的,这样看起来不太好,而且在进一步编辑时容易出错。即使只是一次性使用,给 Params 类型一个名称并重用它可能更有意义。

英文:

To do this you need to tell compiler type of Params again in the literal:

	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: struct {
			Format  string   `json:"format"`
			Cmds    []string `json:"cmds"`
			Version int      `json:"version"`
		}{
			Format:  "json",
			Cmds:    []string{"some", "list", "of", "commands"},
			Version: 1,
		},
	}

As you can see it does not look nice and it is error prone for further edits. It probably makes more sense to just give Params type a name and reuse that, even if it is one-off.

答案2

得分: 1

是的,这是可能的,但你必须在复合字面量中重复匿名类型:

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: struct {
        Format  string   `json:"format"`
        Cmds    []string `json:"cmds"`
        Version int      `json:"version"`
    }{
        Format:  "json",
        Cmds:    []string{"some", "list", "of", "commands"},
        Version: 1,
    },
}

声明类型会更容易一些。

英文:

Yes, it is possible, but you must repeat the anonymous type in the composite literal:

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: struct {
		Format  string   `json:"format"`
		Cmds    []string `json:"cmds"`
		Version int      `json:"version"`
	}{
		Format:  "json",
		Cmds:    []string{"some", "list", "of", "commands"},
		Version: 1,
	},
}

It's easier to declare the type.

huangapple
  • 本文由 发表于 2022年9月10日 23:35:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/73673093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定