经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
Go string 一清二楚
来源:cnblogs  作者:YahuiAn  时间:2021/2/18 15:35:37  对本文有异议

gopher

前言

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

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

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

是什么

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

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

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

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

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

怎么用

  1. func main() {
  2. // 使用字符串字面量初始化
  3. var a = "hi,狗"
  4. fmt.Println(a)
  5. // 可以使用下标访问,但不可修改
  6. fmt.Printf("a[0] is %d\n", a[0])
  7. fmt.Printf("a[0:2] is %s\n", a[0:2])
  8. // a[0] = 'a' 编译报错,Cannot assign to a[0]
  9. // 字符串拼接
  10. var b = a + "狗"
  11. fmt.Printf("b is %s\n", b)
  12. // 使用内置 len() 函数获取其长度
  13. fmt.Printf("a's length is: %d\n", len(a))
  14. // 使用 for;len 遍历
  15. for i := 0; i < len(a); i++ {
  16. fmt.Println(i, a[i])
  17. }
  18. // 使用 for;range 遍历
  19. for i, v := range a {
  20. fmt.Println(i, v)
  21. }
  22. }
  23. /* output
  24. hi,狗
  25. a[0] is 104
  26. a[0:2] is hi
  27. b is hi,狗狗
  28. a's length is: 6
  29. 0 104
  30. 1 105
  31. 2 44
  32. 3 231
  33. 4 139
  34. 5 151
  35. 0 104
  36. 1 105
  37. 2 44
  38. 3 29399
  39. */

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

只读

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

  1. func main() {
  2. var a = "hello"
  3. fmt.Println(a, &a, (*reflect.StringHeader)(unsafe.Pointer(&a)))
  4. a = "world"
  5. fmt.Println(a, &a, (*reflect.StringHeader)(unsafe.Pointer(&a)))
  6. var b = "hello"
  7. fmt.Println(b, &b, (*reflect.StringHeader)(unsafe.Pointer(&b)))
  8. }
  9. /* output
  10. 字符串字面量 该变量的内存地址 底层字节切片
  11. hello 0xc0000381f0 &{5033779 5}
  12. world 0xc0000381f0 &{5033844 5}
  13. hello 0xc000038220 &{5033779 5}
  14. */

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

for;len 遍历

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

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

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

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 的类型别名

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

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

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

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

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

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

参考

Strings, bytes, runes and characters in Go

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

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

原文链接:http://www.cnblogs.com/yahuian/p/14407700.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号