在Go语言中,是否可以使用接口复合字面量?

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

Is it possible to have interface composite literals in Go?

问题

我只想确认一下我对interface{}{}的理解是否正确。

interface{}{}表示接口类型的复合字面值吗?

所以,如果我想将一个复合类型(比如[]byte)作为interface{}传递,我应该将它赋值给一个类型为interface{}{}的变量,并将该变量传递进去;而如果我想将一个非复合类型(比如int)作为interface{}传递,我应该将它赋值给一个类型为interface{}{}的变量,并将该变量传递进去。

我对此的理解正确吗?

英文:

I just want to confirm if my understanding is correct about interface{}{}

Does interface{}{} mean a composite literal of interface type?

So, if I wanted to pass a composite type, lets say []byte as a interface{}, I should assign it to a variable of type interface{}{} and pass in that variable, whereas if I wanted to pass a non composite type, such as a int as a interface{}, I should assign it to a variable of type interface{} and pass in that variable.

Is my understanding correct on this?

答案1

得分: 2

interface{}{} 是无效的语法。

它不是一个空接口的复合字面量 - 这样的东西是不存在的。以下代码是无法编译通过的:

var a interface{}{} = "foo" // 不是一个类型
b := interface{}{}          // 不是一个值

根据规范 Composite Literals

> 复合字面量用于构造 结构体、数组、切片和映射 的值,并在每次评估时创建一个新值。

有效的语法是 interface{},它是空接口,即没有名称和空方法集的接口。

如果将其与命名接口进行比较,你会发现末尾的额外花括号是没有意义的:

type Fooer interface {
    Foo() error
}{} // ???

你可以通过将 nil 转换为 (interface{}) 来实例化一个空的 interface{}

a := (interface{})(nil)

或者你可以声明一个未命名接口类型的变量:

type A struct {}

func (a A) Foo() string {
    return "foo"
}

var a interface{ Foo() string } = A{}

回答你的问题:

> 那么,如果我想传递一个复合类型 [...]

你可以将任何值赋给类型为 interface{} 的变量,就是这样。无论该值是否为复合类型都无关紧要,因为所有类型都满足空接口:

var a interface{} = []byte{0x01, 0x02}
var b interface{} = "string_literal"

Playground: https://play.golang.org/p/w-l1dU-6Hb5

英文:

interface{}{} is invalid syntax.

It is not an empty interface composite literal — there's no such thing. The following don't compile:

var a interface{}{} = "foo" // not a type
b := interface{}{}          // not a value

From the specs Composite Literals:

> Composite literals construct values for structs, arrays, slices, and maps and create a new value each time they are evaluated.

The valid syntax is interface{}, which is the empty interface, i.e. an interface with no name and empty method set.

If you compare it to a named interface, you will see that the extra set of curly braces at the end makes no sense:

type Fooer interface {
    Foo() error
}{} // ???

You can instantiate an empty interface{} by converting nil:

a := (interface{})(nil)

Or you can declare a var of unnamed interface type:

type A struct {}

func (a A) Foo() string {
    return "foo"
}

var a interface{ Foo() string } = A{}

<hr>

To answer your question:

> So, if I wanted to pass a composite type [...]

you can assign any value to a variable of type interface{}, that's about it. Whether the value is composite or not is irrelevant because all types satisfy the empty interface:

var a interface{} = []byte{0x01, 0x02}
var b interface{} = &quot;string_literal&quot;

Playground: https://play.golang.org/p/w-l1dU-6Hb5

答案2

得分: 0

  1. interface{} 是空接口的类型。
  2. 你可以将 []byteint 赋值给任何空接口变量(interface{} 类型),或者直接将其传递给接受接口值的函数。
英文:
  1. interface{} type of empty interface.
  2. you assign []byte or int to any empty variable of empty interface (interface{} type) or you can pass it directly to function that exepts interface values.

答案3

得分: 0

空接口interface{}本质上表示“我不关心”。任何东西都可以作为空接口的参数。所以对于你的例子,它们都可以被存储为空接口。

也许一个更有趣的问题是为什么你想要避免类型,并且你想要实现什么。但是就使用interface{}而言,你可以传递任何东西给它,甚至是“nil”。

英文:

The empty interface interface{} essentially says "I don't care". Anything can be passed as an empty interface. So for your examples they all could be stored as the empty interface.

Possibly a more interesting question is why you want to avoid types in the first place, and what you're trying to achieve. But for the purposes of using interface{} you can pass anything to it, even a "nil".

huangapple
  • 本文由 发表于 2021年8月4日 18:46:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/68649879.html
匿名

发表评论

匿名网友

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

确定