map of integer -> 2d slice in Go

huangapple go评论108阅读模式
英文:

map of integer -> 2d slice in Go

问题

我想要一个将整数映射到具有不同维度的切片的地图。

var SIZE_TO_PERM = make(map[int][][]uint32, 3)

var THREE_C_THREE = [...][3]int {
    {0, 1, 2},
}

var FOUR_C_THREE = [...][3]int {
    {0, 1, 2}, {0, 1, 3}, {0, 3, 2}, {3, 1, 2},
}

var FIVE_C_THREE = [...][3]int {
    // ... 等等
}

func init() {
    SIZE_TO_PERM = map[int][][]uint32 {
        3 : THREE_C_THREE,
        4 : FOUR_C_THREE,
        5 : FIVE_C_THREE,
    }
}

但是这样不起作用,因为Go会抛出错误:

# command-line-arguments
./test.go:96: cannot use THREE_C_THREE (type [1][5]int) as type [][]uint32 in map value
./test.go:97: cannot use FOUR_C_THREE (type [4][5]int) as type [][]uint32 in map value
./test.go:98: cannot use FIVE_C_THREE (type [20][5]int) as type [][]uint32 in map value

我该如何解决这个问题?也许我可以让Go将一个整数映射到一个指针/引用,指向一个地图?这样所有的类型都将是相同的,我只需要跟随地址来获取地图。问题是我事先不知道会找到哪种类型...

对于Go来说都是新的,所以任何提示都会很有帮助。

英文:

I want to have a map of integers to slices with different dimensions.

var SIZE_TO_PERM = make(map[int][][]uint32, 3)

var THREE_C_THREE = [...][3]int {
    {0, 1, 2},
}

var FOUR_C_THREE = [...][3]int {
    {0, 1, 2}, {0, 1, 3}, {0, 3, 2}, {3, 1, 2},
}

var FIVE_C_THREE = [...][3]int {
    // ... etc
}

func init() {
    SIZE_TO_PERM = map[int][][]uint32 {
        3 : THREE_C_THREE,
        4 : FOUR_C_THREE,
        5 : FIVE_C_THREE,
    }
}

But this doesn't work, as Go throws errors:

# command-line-arguments
./test.go:96: cannot use THREE_C_THREE (type [1][5]int) as type [][]uint32 in map value
./test.go:97: cannot use FOUR_C_THREE (type [4][5]int) as type [][]uint32 in map value
./test.go:98: cannot use FIVE_C_THREE (type [20][5]int) as type [][]uint32 in map value

How can I get around this? Perhaps I could somehow have Go map an int to a pointer/reference to a map? Then all the types would be the same, I'd just have to follow the address to get the map there. The problem is I wouldn't know apriori which type I'd find there...

All new to Go, so any tips appreciated.

答案1

得分: 4

首先,切片数组之间有一些区别。

var a [3]int // 3个int类型的数组
var s []int // int类型的切片

在你的情况下,map可以存储切片,但数据存储在不同类型的数组中。

数组的大小是其类型的一部分。由于Go语言的严格类型限制,你不能将一个不同类型/大小的数组的值赋给另一个数组。

相反,我们有切片。切片使用底层数组,但长度和容量可以不同。

你可以通过修改你的代码来解决问题:

var THREE_C_THREE = [][]uint32 { // 将类型从[1][3]int改为[][]uint32
    {0, 1, 2},
}

var FOUR_C_THREE = [][]uint32 { // 将类型从[4][3]int改为[][]uint32
    {0, 1, 2}, {0, 1, 3}, {0, 3, 2}, {3, 1, 2},
}

var FIVE_C_THREE = [][]uint32 { // 将类型从[5][3]int改为[][]uint32
    // ... 等等
}

这篇官方博文会给你更多信息:http://blog.golang.org/slices
还有这篇博文:http://blog.golang.org/go-slices-usage-and-internals

英文:

First of all, there is a difference between a slice and an array.

var a [3]int // Array of 3 ints
var s []int // Slice of ints

In your case, the map can store slices, but the data is stored in different types of arrays.

The size of an array is part of its type. And because of Go's strict typing, you cannot set a value of one array type/size to a value of a different array type/size.

Instead we have slices. Slices uses an underlying array, but may differ in length and capacity.

You can solve your problem by changing your code:

var THREE_C_THREE = [][]uint32 { // Changed type from [1][3]int to [][]uint32
    {0, 1, 2},
}

var FOUR_C_THREE = [][]uint32 { // Changed type from [4][3]int to [][]uint32
    {0, 1, 2}, {0, 1, 3}, {0, 3, 2}, {3, 1, 2},
}

var FIVE_C_THREE = [][]uint32 { // Changed type from [5][3]int to [][]uint32
    // ... etc
}

This official blog-post will give you more info: http://blog.golang.org/slices
And this post as well: http://blog.golang.org/go-slices-usage-and-internals

huangapple
  • 本文由 发表于 2014年2月7日 15:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/21621547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定