什么是“...”意味着在go函数声明中的参数旁边? [英] What does "..." mean when next to a parameter in a go function declaration?

查看:125
本文介绍了什么是“...”意味着在go函数声明中的参数旁边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  func Statusln(a。使用Google Go语言编写的代码。 ..interface {})
func Statusf(格式字符串,a ... interface {})

我不明白 ... 是什么意思。有人知道吗?

解决方案

这意味着你可以用可变数量的参数。例如,使用以下命令调用该函数:

  Statusln(hello,world,42)
code>

将为参数赋予以下值:

  a:= []界面{} {hello,world,42} 

因此,您可以遍历该片段a并处理所有参数,无论有多少个参数。例如,fmt.Printf()采用格式字符串和可变数量的参数,这些参数将根据格式字符串进行格式化,这是一个很好而流行的可变参数用法。


I was going through some code written in Google's Go language, and I came across this:

func Statusln(a ...interface{})
func Statusf(format string, a ...interface{})

I don't understand what the ... means. Does anybody know?

解决方案

It means that you can call Statusln with a variable number of arguments. For example, calling this function with:

Statusln("hello", "world", 42)

Will assign the parameter a the following value:

a := []interface{}{"hello", "world", 42}

So, you can iterate over this slice a and process all parameters, no matter how many there are. A good and popular use-case for variadic arguments is for example fmt.Printf() which takes a format string and a variable number of arguments which will be formatted according to the format string.

这篇关于什么是“...”意味着在go函数声明中的参数旁边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆