将值插入到 `type abc [][]interface{}` 中。

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

Inserting values into `type abc [][]interface{}`

问题

我正在尝试将值插入到type abc [][] interface{}中。

我尝试了以下代码:

insert := &abc{{0, {"abc", "def"}}}

这会抛出错误:Invalid composite literal type: interface{}

我还尝试了以下代码:

insert, _ := json.Marshal([][]interface{}{{0, {"abc", "def"}}})

但这也会抛出错误:Invalid composite literal type: interface{}

我希望输出的结果如下:

[ [ 0, [ "abc", "def" ] ] ]

你能告诉我我做错了什么以及如何解决这个问题吗?

英文:

I am trying to insert values into type abc [][] interface{}

I tried this:

insert := &abc{{0, {"abc", "def"}}}

This throws the error: Invalid composite literal type: interface{}

I also tried this:

insert, _ := json.Marshal([][]interface{}{{0, {"abc", "def"}}})

But this also throws the error Invalid composite literal type: interface{}

I want the output to look like:

[ [ 0, [ "abc", "def" ] ] ]

Could you let me know where I am going wrong and how to solve this?

答案1

得分: 0

abc的类型允许任何类型,但是你放入其中的东西的类型需要被定义。你没有定义{"abc", "def"}的类型,并且你不能实例化一个接口字面量(接口对编译器不提供任何关于字段的信息)。你可以有匿名结构体,但是它们仍然需要被定义。

让我们逐步解释一下你的例子:

x := &abc{}

创建了一个空的外部数组。

x := &abc{{}, {}, {}}

创建了一个外部数组,其中包含3个空的内部数组。

x := &abc{{0, "abc"}}

一个外部数组,其中包含一个内部数组,有两个值:0"abc"

x := &abc{{0, {"abc", "def"}}

一个外部数组,其中包含一个内部数组,有两个值:0和...某个东西?这是一个对象字面量,但是没有显式类型,也没有语法上正确的隐式类型(隐式地期望一个interface{},它不能直接实例化),所以会报错。

type PairOfStrings struct {
  A string
  B string
}

x := &abc{{0, PairOfStrings{"abc", "def"}}}

你现在给这个有问题的结构体指定了类型。

x := &abc{{0, struct {
  A string
  B string
}{"abc", "def"}}}

匿名结构体也是一个丑陋但有效的方法。

x := &abc{{0, []string{"abc", "def"}}}

看起来这是最接近你想要的东西。

规则是interface{}可以容纳任何已定义的东西。它不等同于像C#的dynamic对象那样的东西。

英文:

The type of abc allows for any types however the types of the things you're putting into it need to be defined. You're not defining what the type of {"abc", "def"} is and you can't instantiate an interface literal (interfaces don't tell the compiler anything about fields). You can have anonymous structs, but they still need to be defined.

Let's step through examples towards what you have:

x := &abc{}

Creates the outer array with zero entries.

x := &abc{{}, {}, {}}

Creates the outer array with 3 empty, inner arrays.

x := &abc{{0, "abc"}}

An outer array, with a single inner array, with 2 values: 0 and "abc".

x := &abc{{0, {"abc", "def"}}

An outer array, with a single inner array, with 2 values: 0 and ... something? This is an object literal, but without an explicit type and without a syntactically correct implicit type (implicity it's expecting a interface{} which cannot be directly instantiated) it is an error.

type PairOfStrings struct {
  A string
  B string
}

x := &abc{{0, PairOfStrings{"abc", "def"}}}

You've now typed the problematic struct.

x := &abc{{0, struct {
  A string
  B string
}{"abc", "def"}}}

Anonymous structs are an ugly but valid approach as well.

x := &abc{{0, []string{"abc", "def"}}}

Seems to be the closest thing to what you want though.

The rule is that interface{} can hold any defined thing. It's not equivalent to something like C#'s dynamic objects.

答案2

得分: 0

你可以使用"make"来解决这个问题,例如:

type abc [][]interface{}

v := make([][]interface{}, 0)
v2 := make([]interface{}, 0)

v2 = append(v2, 5)
v = append(v, v2)
a := abc(v)
fmt.Println(a)

英文:

You resolve too with "make", example:

type abc [][]interface{}

v := make([][]interface{}, 0)
v2 := make([]interface{}, 0)

v2 = append(v2, 5)
v = append(v, v2)
a := abc(v)
fmt.Println(a)

huangapple
  • 本文由 发表于 2023年3月17日 01:38:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75759780.html
匿名

发表评论

匿名网友

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

确定