在Golang的圆点圆点。与空括号的接口 [英] dot dot dot in Golang. interface with empty braces
问题描述
这是Golang的一段代码,我有疑问:
这个函数中的a是什么?
func DPrintf(format string,a ... interface {})(n int,err error){
if Debug> 0 {
n,err = fmt.Printf(format,a ...)
}
return
任何人都可以告诉我这是什么点网点?
这个圆点点真的很难to google,想知道它们的含义
Thanks!
参数类型前缀为三个点(.. 。)被称为 variadic参数。这意味着您可以将任何数量或参数传递给该参数(就像 fmt.Printf()
)。该函数将接收参数的参数列表,作为为参数声明的类型的一部分(在您的情况下为 [] interface {}
)。 Go Specification 指出:
函数签名中的最后一个参数的类型前缀为....带有这样一个参数的函数称为variadic,可以用该参数的零个或多个参数来调用。
参数:
a ...界面{}
是,相当于:
a [] interface {}
是你如何将参数传递给这样一个函数。它可以通过分别给每个片段的元素,或者作为一个单独的片段来完成,在这种情况下,你将不得不用三个点来后缀片段值。以下示例将导致相同的调用:
fmt.Println(First,Second,Third)
会做同样的事情:
s:= [] interface {} {First,Second,Third}
fmt.Println(s ...)
这在 Go Specification :
给定函数和调用
func问候语(前缀字符串,who ...字符串)
问候语(nobody)
问候语(hello: ,Joe,Anna,Eileen)
在
,
切片类型who
在第一次调用中的值nil
,<$ c $如果最后一个参数是可赋值的,那么可以使用下面的代码:c> [] string {Joe,Anna,Eileen}[] T
,它可以传递给un如果参数后面紧跟...
,则将其作为... T
参数的值更改。在这种情况下,不会创建新的切片。
给定切片
s
并调用
s:= [] string {James,Jasmine}
问候(goodbye:,s ...)
在
问候语,
谁
与具有相同底层数组的s
具有相同的值。
This is a Golang code piece I have question about: 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 is dot dot dot here?
And what does ...interface{}
do?
This dot dot dot is really hard to google,want to know meanings of them Thanks!
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.
这篇关于在Golang的圆点圆点。与空括号的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!