去可变函数参数传递 [英] Go variadic function argument passing

查看:90
本文介绍了去可变函数参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解,函数中第一个和第二个传递参数有什么区别。在这两种情况下,方法都是功能性的并编译。

<1>


  generateReport(容量...)

func generateReport(capacity ... float64){
for i,cap:= range ranges {
fmt.Printf(Plant%d capacity%.0f\\\
, i,cap)
}
}



<2>

  generateReport(plantCapacities)

func generateReport(capacity [] float64){
for i,cap:=范围内的能力{
fmt.Printf(工厂%d容量%.0f \ n,i,上限)
}
}

已发现少量好样本

1) GolangBot - Variadic Function

2) Golang.org - 将参数传递给@Himanshu提到。

Golang语言规范


如果f是类型为... T的最终参数p的可变参数,则在f
中,p的类型等同于类型[] T。如果对f调用f而没有
的实参,则传递给p的值为零。否则,传递的
值是一个带有新的基础数组
的新类型[] T,其连续元素是实际参数,所有元素都必须是
可分配给T.长度和因此切片的容量是绑定到p的$ ​​b $ b参数数量,并且可能因每个调用站点而不同。


Golang使用可变参数函数来处理多重尾随参数。函数可以用来传递slice参数。

  func main(){
capacity:= [] float64 {1 ,2,3,4}
generateReport(容量...)
}

func generateReport(容量... float64){
for i,cap: =范围容量{
fmt.Printf(工厂%d容量%.0f \ n,i,上限)
}
}

变量函数也可以通过单独的参数以通常的方式调用。它的工作原理类似于java脚本中的扩展运算符,它可以带多个参数例如: -

  func main(){
generateReport(1,2,3,4)
}

func generateReport(capacity ... float64){
for i,cap:= range ranges {
fmt.Printf(Plant%d capacity%.0f\\ \\ n,i,cap)
}
}


I am trying to understand, What is difference between 1st and 2nd passing argument in function. In both case methods are functional and compiles.

1)

generateReport(capacities...)

func generateReport(capacities ...float64) {
    for i, cap := range capacities {
        fmt.Printf("Plant %d capacity %.0f\n", i, cap)
    }
}

2)

generateReport(plantCapacities)

func generateReport(capacities []float64) {
    for i, cap := range capacities {
        fmt.Printf("Plant %d capacity %.0f\n", i, cap)
    }
}

Have found few good samples

1) GolangBot - Variadic Function

2) Golang.org - Passing arguments as @Himanshu mentioned.

解决方案

According to Golang language specification

If f is variadic with a final parameter p of type ...T, then within f the type of p is equivalent to type []T. If f is invoked with no actual arguments for p, the value passed to p is nil. Otherwise, the value passed is a new slice of type []T with a new underlying array whose successive elements are the actual arguments, which all must be assignable to T. The length and capacity of the slice is therefore the number of arguments bound to p and may differ for each call site.

Golang uses variadic functions to handle multple trailing arguments. Function can be used to pass slice arguments.

func main(){
  capacities := []float64{1, 2, 3, 4}
  generateReport(capacities...)
}

func generateReport(capacities ...float64) {
    for i, cap := range capacities {
        fmt.Printf("Plant %d capacity %.0f\n", i, cap)
    }
}

Variadic functions can also be called in usual way with individual arguments. It works like spread operator in java script which can take multiple arguments. For eg:-

func main(){
  generateReport(1,2,3,4)
}

func generateReport(capacities ...float64) {
    for i, cap := range capacities {
        fmt.Printf("Plant %d capacity %.0f\n", i, cap)
    }
}

这篇关于去可变函数参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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