doumeitang572461 2017-11-05 10:51
浏览 76
已采纳

GoLang:返回任意类型的2d切片

I know how to create a 2D slice like this.

var data []int
data = make([]int, w*h)
v := make([][]int, h)
for i := 0; i < h; i++ {
    v[i] = data[w*i : w*(i+1)]
}

Since this is very verbose and I'm going to create many of these, I decided to refactor it into a function.

func create2dSlice(w, h int) [][]int {
    var data []int
    data = make([]int, w*h)
    v := make([][]int, h)
    for i := 0; i < h; i++ {
        v[i] = data[w*i : w*(i+1)]
    }
    return v
}

func main() {
    a := create2dSlice(3, 2)
}

This works only for integers. It there any way in golang to do this for other types reusing the same code?

I come from C++ and I would expect to be able to do something like this.

create2dSlice<int>(w, h)
  • 写回答

3条回答 默认 最新

  • douzhechi2435 2017-11-05 21:41
    关注

    Go doesn't have generics. For matrices, a similar problem, I have a NewMatrix function in a matrix.go file in my snippet folder. By design, I can simply copy it and do a global change of []int to another type, for example, []float64.

    You could improve your function by giving w slices a valid capacity.

    For example,

    package main
    
    import "fmt"
    
    func NewMatrix(r, c int) [][]int {
        a := make([]int, c*r)
        m := make([][]int, r)
        lo, hi := 0, c
        for i := range m {
            m[i] = a[lo:hi:hi]
            lo, hi = hi, hi+c
        }
        return m
    }
    
    func create2dSlice(w, h int) [][]int {
        a := make([]int, w*h)
        s := make([][]int, h)
        lo, hi := 0, w
        for i := range s {
            s[i] = a[lo:hi:hi]
            lo, hi = hi, hi+w
        }
        return s
    }
    
    func main() {
        r, c := 2, 3
        m := NewMatrix(r, c)
        fmt.Println(m)
        w, h := c, r
        a := create2dSlice(w, h)
        fmt.Println(a)
    }
    

    Output:

    [[0 0 0] [0 0 0]]
    [[0 0 0] [0 0 0]]
    

    The Go Programming Language Specification

    Slice expressions

    Slice expressions construct a substring or slice from a string, array, pointer to array, or slice. There are two variants: a simple form that specifies a low and high bound, and a full form that also specifies a bound on the capacity.

    Full slice expressions

    For an array, pointer to array, or slice a (but not a string), the primary expression

    a[low : high : max]
    

    constructs a slice of the same type, and with the same length and elements as the simple slice expression a[low : high]. Additionally, it controls the resulting slice's capacity by setting it to max - low.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答