前言

字符串(string) 作为 go 语言的基本数据类型,在开发中必不可少,我们务必深入学习一下,做到一清二楚。

本文假设读者已经知道切片(slice)的使用,如不了解,可阅读 Go 切片 基本知识点

为了更好的理解后文,推荐先阅读 Unicode 字符集,UTF-8 编码

是什么

In Go, a string is in effect a read-only slice of bytes.

在 go 语言中,字符串实际上是一个只读字节切片,其数据结构定义如下:

// runtime/string.go
type stringStruct struct {
str unsafe.Pointer // 指向底层字节数组的指针
len int // 字节数组的长度
}

注意:byte 其实是 uint8 的类型别名

// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8

怎么用

func main() {
// 使用字符串字面量初始化
var a = "hi,狗"
fmt.Println(a) // 可以使用下标访问,但不可修改
fmt.Printf("a[0] is %d\n", a[0])
fmt.Printf("a[0:2] is %s\n", a[0:2])
// a[0] = 'a' 编译报错,Cannot assign to a[0] // 字符串拼接
var b = a + "狗"
fmt.Printf("b is %s\n", b) // 使用内置 len() 函数获取其长度
fmt.Printf("a's length is: %d\n", len(a)) // 使用 for;len 遍历
for i := 0; i < len(a); i++ {
fmt.Println(i, a[i])
} // 使用 for;range 遍历
for i, v := range a {
fmt.Println(i, v)
}
} /* output
hi,狗 a[0] is 104
a[0:2] is hi b is hi,狗狗 a's length is: 6 0 104
1 105
2 44
3 231
4 139
5 151 0 104
1 105
2 44
3 29399
*/

如果读者在看上面的代码时有疑惑,不用着急,下文将会挨个解读。

只读

字符串常量会在编译期分配到只读段,对应数据地址不可写入,相同的字符串常量不会重复存储

func main() {
var a = "hello"
fmt.Println(a, &a, (*reflect.StringHeader)(unsafe.Pointer(&a)))
a = "world"
fmt.Println(a, &a, (*reflect.StringHeader)(unsafe.Pointer(&a)))
var b = "hello"
fmt.Println(b, &b, (*reflect.StringHeader)(unsafe.Pointer(&b)))
} /* output
字符串字面量 该变量的内存地址 底层字节切片
hello 0xc0000381f0 &{5033779 5}
world 0xc0000381f0 &{5033844 5}
hello 0xc000038220 &{5033779 5}
*/

可以看到 hello 在底层只存储了一份

for;len 遍历

go 的源代码都是 UTF-8 编码格式的,上例中的”狗“字占用三个字节,即 231 139 151(Unicode Character Table),所以上例的运行结果很清楚。

于此同时,也可以将字符串转化为字节切片

func main() {
var a = "hi,狗"
b := []byte(a)
fmt.Println(b) // [104 105 44 231 139 151]
}

for;range 遍历

The Unicode standard uses the term "code point" to refer to the item represented by a single value.

在 Unicode 标准中,使用术语 code point 来表示由单个值表示的项,通俗点来说,U+72D7(十进制表示为 29399)代表符号 ”狗“

"Code point" is a bit of a mouthful, so Go introduces a shorter term for the concept: rune.

code point 有点拗口,所以在 go 语言中专门有一个术语来代表它,即 rune

注意:rune 其实是 int32 的类型别名

// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32

在对字符串类型进行 for;range 遍历时,其实是按照 rune 类型来解码的,所以上例的运行结果也很清晰。

与此同时,也可以将字符串转化为 rune 切片

func main() {
// 使用字符串字面量初始化
var a = "hi,狗"
r := []rune(a)
fmt.Println(r) // [104 105 44 29399]
}

当然我们也可以使用 "unicode/utf8" 标准库,手动实现 for;range 语法糖相同的效果

func main() {
var a = "hi,狗"
for i, w := 0, 0; i < len(a); i += w {
runeValue, width := utf8.DecodeRuneInString(a[i:])
fmt.Printf("%#U starts at byte position %d\n", runeValue, i)
w = width
}
} /* output
U+0068 'h' starts at byte position 0
U+0069 'i' starts at byte position 1
U+002C ',' starts at byte position 2
U+72D7 '狗' starts at byte position 3
*/

参考

Strings, bytes, runes and characters in Go

为什么说go语言中的string是不可变的?

字符咋存?utf8咋编码?string啥结构?

最新文章

  1. U3D 打包时找不到tag的问题
  2. ZJOI2014 2048
  3. 网络数据的XML解析
  4. 51nod1258 序列求和V4
  5. Android之完美退出方法
  6. CI框架多目录设置
  7. SQL Server AlwaysON从入门到进阶(2)——存储
  8. AdminIII连接linux Postgresql过程中的几个小问题
  9. CentOS 7下systemd是如何stop mysql服务的
  10. BZOJ1178 APIO2009 会议中心 贪心、倍增
  11. 2018年web最新面试知识点总结
  12. The Mathematics of the Rubik’s Cube
  13. MATLAB的一些小经验,记下来,facilitate future work
  14. layui之初始化加分页重复请求问题解决
  15. .net项目的mvc简单发布
  16. php session阻塞页面分析及优化 (session_write_close session_commit使用)
  17. 算法笔记_117:算法集训之结果填空题集一(Java)
  18. iOS 图片部分模糊,类似于美图秀秀
  19. js中this那些事儿
  20. Matlab绘图基础——图形绘制的插值&#160; 以及 图像大小的重采样

热门文章

  1. 前端知识(二)01-NPM包管理器-谷粒学院
  2. 关于Mysql数据库建库字符集utf8mb4下,排序规则utf8mb4_bin和utf8mb4_general_ci选择造成的查询匹配大小写问题
  3. 【AtCoder Beginner Contest 181】A~F题解
  4. Promise用法
  5. LINUX动态库(.SO)搜索路径(目录)设置方法
  6. 动态库与静态库的学习 博主写的很好 静态库 编译的时候 需要加上 static 动态库编译ok运行不成功就按照文章中的方法修改
  7. 成为一名优秀的Java程序员9+难以置信的公式
  8. 时间模块,os模块,sys模块
  9. vmware打开虚拟级断电情况下,无法找到虚拟机文件
  10. python——模块、标准库、第三方模块安装