英文:
Hash with key as an array type
问题
在Go语言中,可以使用数组作为map的键。以下是在Go中实现相同功能的示例代码:
package main
import "fmt"
func main() {
quarters := map[[3]int]string{
[3]int{1, 2, 3}: "First quarter",
[3]int{4, 5, 6}: "Second quarter",
[3]int{7, 8, 9}: "Third quarter",
[3]int{10, 11, 12}: "Fourth quarter",
}
fmt.Println(quarters[[3]int{1, 2, 3}])
// 输出:"First quarter"
}
在这个示例中,我们使用[3]int
作为数组的类型,表示键的类型是一个包含3个整数的数组。然后,我们创建一个quarters
的map,将数组作为键,对应的字符串作为值。最后,我们可以通过指定相应的数组作为键来访问map中的值。
英文:
How to create a key as array in Go for map.
For example in ruby I can implement it such:
quarters = {
[1, 2, 3] => 'First quarter',
[4, 5, 6] => 'Second quarter',
[7, 8 ,9] => 'Third quarter',
[10, 11, 12] => 'Fourh quarter',
}
quarters[[1, 2, 3]]
# => "First quarter"
How the same will be looked in Golang ?
答案1
得分: 8
Go中的数组类型(与切片不同)是可比较的,所以其中没有什么神奇的地方:你可以像定义其他映射一样定义它:map[KeyType]ValueType
,其中KeyType
将是[3]int
,ValueType
将是string
。
对于键类型的操作数,必须完全定义比较运算符
==
和!=
;因此,键类型不能是函数、映射或切片。
m := map[[3]int]string{}
m[[3]int{1, 2, 3}] = "First quarter"
m[[3]int{4, 5, 6}] = "Second quarter"
m[[3]int{7, 8, 9}] = "Third quarter"
m[[3]int{10, 11, 12}] = "Fourth quarter"
fmt.Println(m)
输出:
map[[1 2 3]:First quarter [4 5 6]:Second quarter
[7 8 9]:Third quarter [10 11 12]:Fourth quarter]
在Go Playground上尝试一下。
要查询一个元素:
fmt.Println(m[[3]int{1, 2, 3}]) // 输出"First quarter"
你也可以一步创建映射:
m := map[[3]int]string{
[3]int{1, 2, 3}: "First quarter",
[3]int{4, 5, 6}: "Second quarter",
[3]int{7, 8, 9}: "Third quarter",
[3]int{10, 11, 12}: "Fourth quarter",
}
英文:
Array types (unlike slices) in Go are comparable, so there is nothing magical in it: you can just define it like any other maps: map[KeyType]ValueType
where KeyType
will be [3]int
and ValueType
will be string
.
> The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice.
m := map[[3]int]string{}
m[[3]int{1, 2, 3}] = "First quarter"
m[[3]int{4, 5, 6}] = "Second quarter"
m[[3]int{7, 8, 9}] = "Third quarter"
m[[3]int{10, 11, 12}] = "Fourth quarter"
fmt.Println(m)
Output:
map[[1 2 3]:First quarter [4 5 6]:Second quarter
[7 8 9]:Third quarter [10 11 12]:Fourth quarter]
Try it on the Go Playground.
To query an element:
fmt.Println(m[[3]int{1, 2, 3}]) // Prints "First quarter"
You can also create the map in one step:
m := map[[3]int]string{
[3]int{1, 2, 3}: "First quarter",
[3]int{4, 5, 6}: "Second quarter",
[3]int{7, 8, 9}: "Third quarter",
[3]int{10, 11, 12}: "Fourth quarter",
}
答案2
得分: 1
需要注意的是,Ruby中的array
和Go中的array
是不同的数据结构。Go的数组是不可变的,而Ruby的array
是可变的。Go的slice
是更类似于Ruby的array
的数据结构。然而,正如icza的回答所指出的那样,在Go中不能将slices
用作键。
所以回答你的问题:
在Go中会是怎样的呢?
这是不可能的,在Go中你不能有一个将动态数组
映射到字符串
的字典。然而,你可以将slices
转换为arrays
并将它们用作键,icza已经很好地解释了这一点。
英文:
It should be noted that array
in ruby and array
in go are different data structures. Go array is not mutable, while ruby array
is. Go slice
is a lot more similar data structure to ruby array
. However, you cannot use slices
as keys in go, as pointed out in icza's answer.
So to answer your question:
> How the same will be looked in Golang ?
It is impossible, you cannot have a dictionary that maps dynamic arrays
to strings
in go. However, you could convert slices
to arrays
and use them as keys, as very well explained by icza.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论