larry*wei 2013-04-27 04:08 采纳率: 0%
浏览 450
已采纳

连接两个切片到 Go

I'm trying to combine the slice [1, 2] and the slice [3, 4]. How can I do this in Go?

I tried:

append([]int{1,2}, []int{3,4})

but got:

cannot use []int literal (type []int) as type int in append

However, the documentation seems to indicate this is possible, what am I missing?

slice = append(slice, anotherSlice...)

转载于:https://stackoverflow.com/questions/16248241/concatenate-two-slices-in-go

  • 写回答

6条回答 默认 最新

  • 斗士狗 2014-09-26 15:34
    关注

    Add dots after the second slice:

    //---------------------------vvv
    append([]int{1,2}, []int{3,4}...)
    

    This is just like any other variadic function.

    func foo(is ...int) {
        for i := 0; i < len(is); i++ {
            fmt.Println(is[i])
        }
    }
    
    func main() {
        foo([]int{9,8,7,6,5}...)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大