...interface{} 的含义(dot dot dot interface) [英] Meaning of ...interface{} (dot dot dot interface)
问题描述
下面是一段我有疑问的 Go 代码.具体来说,这个函数中的a
是什么?
func DPrintf(format string, a ...interface{}) (n int, err error) {如果调试>0 {n, err = fmt.Printf(format, a...)}返回}
谁能告诉我这里的三个点是什么?...interface{}
有什么作用?
以三个点 (...) 为前缀的参数类型称为可变参数.这意味着您可以将任何数字或参数传递给该参数(就像使用 fmt.Printf()
一样).该函数将接收参数的参数列表作为为参数声明的类型的切片(在您的情况下为[]interface{}
).Go 规范 指出:
函数签名中的最后一个参数的类型可能以 ... 为前缀.具有这种参数的函数称为可变参数,可以使用该参数的零个或多个参数调用.
一个参数:
一个 ...接口{}
是,对于等价于的函数:
一个[]接口{}
区别在于您如何将参数传递给这样的函数.它可以通过分别提供切片的每个元素或作为单个切片来完成,在这种情况下,您必须使用三个点作为切片值的后缀.以下示例将产生相同的调用:
fmt.Println(第一"、第二"、第三")
会做同样的事情:
s := []interface{}{First", Second", Third"}fmt.Println(s...)
这在 Go 规范 中也有很好的解释:><块引用>
给定函数和调用
func Greeting(prefix string, who ...string)问候(没人")问候(你好:",乔",安娜",艾琳")
在 Greeting
中,who
在第一次调用时将具有 nil
值,而 []string{Joe"、安娜"、艾琳"}
在第二个.
如果最终参数可分配给切片类型 []T
,如果参数是后跟 ...
.在这种情况下,不会创建新切片.
给定切片 s
并调用
s := []string{James", Jasmine"}问候(再见:",s ...)
在 Greeting
中,who
将与 s
具有相同的值,并且具有相同的底层数组.
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?
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 valuenil
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 calls := []string{"James", "Jasmine"} Greeting("goodbye:", s...)
within
Greeting
,who
will have the same value ass
with the same underlying array.
这篇关于...interface{} 的含义(dot dot dot interface)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!