我有这个文本,我想把它放到一个字节片中:
s := "There are these two young fish swimming along and they happen to meet an older fish swimming the other way"
如果我写
b := []byte("There are these two young fish swimming along and they happen to meet an older fish swimming the other way")
据我了解,在运行时这将:
用内存中的值创建一个字符串
创建一个字节切片
将字符串的内容复制到字节片中(根据需要重新分配)
我可以将每个字符串值转换为它们的 ASCII 等效值并直接创建字节切片:
b := []byte{84, 104, ... }
虽然这不是很可读。
我知道这里的例子有点微不足道,大多数计算机都可以在一瞬间完成,但我对此很好奇。编译器是否在编译时[]byte("blah")将其解释并转换为有效的字节片?如果字符串包含非 ASCII 字符,最佳解决方案会改变吗?
繁花如伊
至尊宝的传说
相关分类