…interface{}(点 点 点 接口)的含义是什么?

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

Meaning of ...interface{} (dot dot dot interface)

问题

以下是我对你提供的Go代码的翻译:

func DPrintf(format string, a ...interface{}) (n int, err error) {
  if Debug > 0 {
    n, err = fmt.Printf(format, a...)
  }
  return
}

在这个函数中,a 是一个可变参数。在Go语言中,可变参数使用 ... 表示。...interface{} 表示参数 a 可以接受任意数量的参数,并且这些参数的类型可以是任意类型。在函数体内部,可以通过 a... 将可变参数传递给其他函数或方法。

希望对你有帮助!如果你还有其他问题,请随时提问。

英文:

Below is a piece of Go code I have question about.
Specifically, what is a in this function?

func DPrintf(format string, a ...interface{}) (n int, err error) {
  if Debug > 0 {
    n, err = fmt.Printf(format, a...)
  }
  return
}

Could anyone tell me what the three dots are here?
And what does ...interface{} do?

答案1

得分: 194

一个以三个点(...)为前缀的参数类型被称为可变参数。这意味着你可以将任意数量的参数传递给该参数(就像使用fmt.Printf()一样)。函数将以声明参数类型的切片形式(在你的情况下是[]interface{})接收参数的列表作为参数。Go规范中指出:

>函数签名中的最后一个参数可以带有以...为前缀的类型。带有这种参数的函数被称为可变参数函数,可以使用零个或多个参数调用该函数。

一个参数:

a ...interface{}

对于函数来说等同于:

a []interface{}

不同之处在于如何将参数传递给这样的函数。可以通过分别给出切片的每个元素,或者作为单个切片来完成,如果是后者,则需要在切片值后面加上三个点。以下示例将得到相同的调用结果:

fmt.Println("First", "Second", "Third")

与以下代码等效:

s := []interface{}{"First", "Second", "Third"}
fmt.Println(s...)

这在Go规范中也有很好的解释:

>给定函数和调用
>
> func Greeting(prefix string, who ...string)
> Greeting("nobody")
> Greeting("hello:", "Joe", "Anna", "Eileen")
>
>在Greeting函数中,第一次调用时,who的值为nil,第二次调用时,who的值为[]string{"Joe", "Anna", "Eileen"}
>
>如果最后一个参数可以赋值给切片类型[]T,并且在参数后面跟着...,则可以将其不变地作为...T参数的值传递。在这种情况下,不会创建新的切片。
>
>给定切片s和调用
>
> s := []string{"James", "Jasmine"}
> Greeting("goodbye:", s...)
>
>在Greeting函数中,who将具有与s相同的值和相同的底层数组。

英文:

A parameter type prefixed with three dots (...) is called a variadic parameter. That means you can pass any number or arguments into that parameter (just like with fmt.Printf()). The function will receive the list of arguments for the parameter as a slice of the type declared for the parameter ([]interface{} in your case). The Go Specification states:

>The final parameter in a function signature may have a type prefixed with .... A function with such a parameter is called variadic and may be invoked with zero or more arguments for that parameter.

A parameter:

a ...interface{}

Is, for the function equivalent to:

a []interface{}

The difference is how you pass the arguments to such a function. It is done either by giving each element of the slice separately, or as a single slice, in which case you will have to suffix the slice-value with the three dots. The following examples will result in the same call:

fmt.Println("First", "Second", "Third")

Will do the same as:

s := []interface{}{"First", "Second", "Third"}
fmt.Println(s...)

This is explained quite well in the Go Specification as well:

> Given the function and calls
>
> func Greeting(prefix string, who ...string)
> Greeting("nobody")
> Greeting("hello:", "Joe", "Anna", "Eileen")
>
> within Greeting, who will have the value nil in the first call, and []string{"Joe", "Anna", "Eileen"} in the second.
>
> If the final argument is assignable to a slice type []T, it may be passed unchanged as the value for a ...T parameter if the argument is followed by .... In this case no new slice is created.
>
> Given the slice s and call
>
> s := []string{"James", "Jasmine"}
> Greeting("goodbye:", s...)
>
>within Greeting, who will have the same value as s with the same underlying array.

答案2

得分: 11

interface{}这个术语而言,它是空接口。换句话说,它是Go语言中所有变量实现的接口。

这有点类似于Java中的java.lang.Object或C#中的System.Object,但它包括语言中的每种变量类型。因此,它允许你将任何东西传递给方法。

英文:

As far as the interface{} term, it is the empty interface. In other words, the interface implemented by all variables in Go.

This is sort of analogous to java.lang.Object or System.Object in C#, but is instead inclusive of every variable type in the language. So it lets you pass in anything to the method.

huangapple
  • 本文由 发表于 2014年5月15日 13:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/23669720.html
匿名

发表评论

匿名网友

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

确定