加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

圆点圆点在Golang 接口与空括号

发布时间:2020-12-16 18:04:18 所属栏目:大数据 来源:网络整理
导读:这是一个Golang代码片我有问题: 什么是“a”在这个函数? func DPrintf(format string,a ...interface{}) (n int,err error) { if Debug 0 { n,err = fmt.Printf(format,a...) } return 有谁能告诉我什么是点点在这里? … interface {}做什么? 这个点阵点
这是一个Golang代码片我有问题:
什么是“a”在这个函数?
func DPrintf(format string,a ...interface{}) (n int,err error) {
  if Debug > 0 {
    n,err = fmt.Printf(format,a...)
  }
  return

有谁能告诉我什么是点点在这里?
… interface {}做什么?

这个点阵点真的很难google,想知道他们的意思
谢谢!

以三个点(…)为前缀的参数类型称为可变参数。这意味着你可以传递任何数字或参数到该参数(就像fmt.Printf())。函数将接收参数的参数列表作为为您的情况下参数([] interface {})声明的类型的切片。 Go Specification状态:

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 ...interface{}

是,对于相当于的函数:

a []interface{}

不同的是如何传递参数到这样的函数。这是通过单独给出切片的每个片段或作为切片来完成的,在这种情况下,您将必须用三个点后缀切片值。以下示例将导致相同的调用:

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

会做同样的:

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

这在Go Specification也很好地解释:

Given the function and call

06004

within Greeting,who will have the value []string{"Joe","Anna","Eileen"}

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

06005

within Greeting,who will have the same value as s with the same underlying array.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读