Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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[]))

  ask by siasalar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 be myFunc(arr [5]int) .

(数组具有恒定的长度并且不能增长,在我的示例中,数组的函数签名为myFunc(arr [5]int) 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...