dtwupu6414 2017-06-11 11:39
浏览 79
已采纳

如何解决函数中的Go数组大小

I just started using Go, and one thing that makes me nervous is that if I define a function that accepts an array, and then pass an array to a function with a specific size, VS Code complains about it.

For example, I have something like this:

func cmplt(a, b []uint64) uint64 {
    var r, m, i uint64
    r = 0
    m = 0

    for i = 2; i >= 0; i-- {
        r |= LtUint64(a[i], b[i]) & ^m
        m |= MaskUint64(NeUint64(a[i], b[i]))
    }

    return r & 1
}

And then I call this function inside another function as:

func singleSample(in []uint64) uint64 {
    var i, index uint64
    index = 0

    for i = 0; i < 52; i++ {
        index = SelectUint64(index, i+1, cmplt(in, table[i]))
    }

    return index
}

where table has type [52][3]uint64. I get an error message saying: cannot use table (type [3]uint64) as []uint64 in argument to cmplt.

Is there any way in Go to workaround this, instead of specifically specifying the array size in function parameter?

  • 写回答

2条回答 默认 最新

  • douxi3977 2017-06-11 11:56
    关注

    Your cmplt is expecting a uint64 slice, and not a 3 element array. Taking a slice of table[i] will fix the error:

    cmplt(in, table[i][:])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊