go - slice 作为 map 中的键

标签 go

是否可以使用 slice 作为键?

这是我的尝试:

h := map[[]string]string{
  []string{"a", "b"} : "ab",
}

编译器给了我一个错误invalid map key type []string。所以要么不可能,要么我声明不正确(如果是这样,正确的方法是什么?)。

最佳答案

但是,它是 possible使用数组作为映射键:

package main

import "fmt"

func main() {
    m := make(map[[2]int]bool)
    m[[2]int{1, 2}] = false
    fmt.Printf("%v", m)
}

关于go - slice 作为 map 中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20297503/

相关文章:

google-app-engine - 从 Google BigQuery 提取结果到云存储 golang

go - 读取文件中的多个 yaml

sql - 我如何从 Golang 中的 Postgres 数组中获取 slice ?

firebase - 如何向 Firestore Go SDK 验证用户身份?

go - 在golang中作为 map 字段插入 map

parsing - 找出调用哪个命令的有效方法?

go - go中的二维 slice 类型转换

mongodb - 计算数组中元素的数量

function - 如何测试在 main() 函数中调用了特定方法

谷歌存储 : how to check if a bucket exists