【问题标题】:In golang, how to return a slice of type int from a function to another function?在golang中,如何将一个int类型的切片从一个函数返回到另一个函数?
【发布时间】:2013-07-13 01:13:17
【问题描述】:

我已经使用渠道进行交流..

   b := make([]int,0)  //This is the slice I have created.

我正在向切片附加值,我想将存储在 b 中的最终切片传输给另一个函数。我已经使用了这段代码..

   slic := make(chan int)
   go func() { slic <- input()} ()
   slice := <-slic
   fmt.Println(slice)

我收到此错误:“不能在返回参数中使用 b (type []int) 作为 int 类型。”

【问题讨论】:

  • 那么 input() 函数是返回一个整数切片还是一个整数?

标签: go


【解决方案1】:

将你的 chan 改成这样:

make(chan []int)

或者选择你的 []int 的索引发送到你的chan int

int[]int 是不同的类型,chan intchan []int 是不同的类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多