I'm looking to find syntax that how can I send an array to use in a function as input in Go.
(我正在寻找一种语法,该语法如何发送数组以用作Go中的输入。)
function UsingArray(a int[])
(函数UsingArray(a int[])
)
I'm looking to find syntax that how can I send an array to use in a function as input in Go.
(我正在寻找一种语法,该语法如何发送数组以用作Go中的输入。)
function UsingArray(a int[])
(函数UsingArray(a int[])
)
You were almost there, here is an example:
(您快到了,这里有个例子:)
package main
import (
"fmt"
)
func myFunc(arr []int) {
fmt.Println(arr)
}
func main() {
var arr = []int{1, 2, 3, 54, 3}
myFunc(arr)
}
Live on playground
(住在操场上)
PS As @torek mentioned, to be precise, you are using a slice not an array.
(PS如@torek所述,确切地说,您使用的是切片而不是数组。)
Arrays have constant length and cannot grow, and the function signature for array in my example would bemyFunc(arr [5]int)
. (数组具有恒定的长度并且不能增长,在我的示例中,数组的函数签名为myFunc(arr [5]int)
。)