arrays - 使用非 UTF-8 编码的字符串作为映射键
问题描述
我想使用可变长度的字节数组作为地图中的键。
myMap := make(map[[]byte]int)
由于切片和可变长度字节数组在 go 中不是有效的键类型,因此上面的代码无效。
然后我读到字符串只是一组 8 位字节,通常但不一定代表 UTF-8 编码的文本。
使用这种非 UTF-8 编码的字符串作为关于散列的映射键有什么问题吗?
以下代码演示了我如何将[]byte转换为字符串并再次转换回[]byte:
package main
import (
"bytes"
"fmt"
)
func main() {
// src is a byte array with all available byte values
src := make([]byte, 256)
for i := 0; i < len(src); i++ {
src[i] = byte(i)
}
fmt.Println("src:", src)
// convert byte array to string for key usage within a map
mapKey := string(src[:]) // <- can this be used for key in map[string]int?
//fmt.Println(mapKey) // <- this destroys the print function!
fmt.Printf("len(mapKey): %d\n", len(mapKey)) // <- that actually works
// convert string back to dst for binary usage
dst := []byte(mapKey)
fmt.Println("dst:", dst)
if bytes.Compare(src, dst) != 0 {
panic("Ups... something went wrong!")
}
}
解决方案
string
在字符串不是有效的 UTF-8 的映射中使用 as 键没有问题。
在 Go 中,字符串实际上是只读的字节片。
字符串值是可比较的和有序的,词法字节。
重要的是有什么字节string
,它可能是有效的还是无效的 UTF-8 序列。如果 2 个string
值具有相同的无效 UTF-8 字节序列,则它们相等,如果不相等,则它们不相等。
测试无效和有效序列("\xff"
和"\x00"
):
m := map[string]byte{}
m["\xff"] = 1
m["\x00"] = 2
fmt.Println(m["\xff"], m["\x00"])
输出是(在Go Playground上试试):
1 2
推荐阅读
- html - 尝试创建列 HTML CSS
- c# - ASP.Net Core:处理 SSH 客户端
- python - 单个文件中的多行结尾
- python - 为什么在将第一组信息分配给数据变量到上下文时,我不断收到键错误?
- google-apps-script - 无法通过 GAS 连接到 Google Cloud SQL
- r - 使用带有 POSIxct 日期的 geom_path 来显示随着时间的推移使用 ggplot2 的轨迹
- java - 将父方法映射到不同类的子类的相同擦除
- visual-studio-code - 如何在 VSCode 的非 python 调试会话期间临时激活 virtualenv?
- ios - 快速将图像添加到工具栏?
- python - 你如何检查矩形中的碰撞?