Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

arrays - 如何将数组发送到函数(How to send an array into function)

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

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

1 Reply

0 votes
by (71.8m points)

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) 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...