golang 二维切片
生活随笔
收集整理的这篇文章主要介绍了
golang 二维切片
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
初始化:
res := make([][length]int, length),
例如:
res := make([][2]int, 10)
fmt.Println(res)
输出:
[[0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0] [0 0]]或者
a := [][]float64{{1, 2, 3, 4},{12, 21, 3, 14},{1, 1, 2, 3},{2, 3, 1, 6},{2, 2, 3, 3},{1, 1, 1, 1}}
排序:
//根据二维切片的第一个属性从大到小排序,第二个属性默认是递增顺序
people := [][]int{{9, 0}, {7, 0}, {1, 9}, {3, 0}, {2, 7}, {5, 3}, {6, 0}, {3, 4}, {6, 2}, {5, 2}}
less := func(i, j int) bool {if people[i][0] == people[j][0] {return people[i][1] < people[j][1]}return people[i][0] > people[j][0]
}
//调用排序函数
sort.Slice(people, less)
fmt.Println(people)
//输出
//[[9 0] [7 0] [6 0] [6 2] [5 2] [5 3] [3 0] [3 4] [2 7] [1 9]]
转载于:https://www.cnblogs.com/nyist-xsk/p/11365412.html
总结
以上是生活随笔为你收集整理的golang 二维切片的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【转】tf中的padding方式SAME
- 下一篇: Go排序