我是靠谱客的博主 会撒娇花瓣,这篇文章主要介绍深入理解 Go 中的字符串,现在分享给大家,希望可以做个参考。

字符串的本质

在编程语言中,字符串发挥着重要的角色。字符串背后的数据结构一般有两种类型:

  • 一种在编译时指定长度,不能修改
  • 一种具有动态的长度,可以修改。

比如:与Python 中的字符串一样,Go 语言中的字符串不能被修改,只能被访问。
在 Python 中,如果改变一个字符串的值会得到如下结果:

复制代码
1
2
3
4
5
6
7
8
>>> hi = "Hello" >>> hi 'Hello' >>> hi[0] = 'h' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>>

同理,在 Go 中也一样:

复制代码
1
2
3
4
5
6
7
8
9
package main import "fmt" func main() { var hello = "Hello" hello[1] = 'h' fmt.Println(hello) } // # command-line-arguments // string_in_go/main.go:8:11: cannot assign to hello[1] (strings are immutable)

字符串的终止方式有两种:

  • 一种是 C 语言的隐式声明,以字符 “” 作为终止符
  • 一种是 Go 语言的显式声明

Go 语言的 string 的表示结构如下:

复制代码
1
2
3
4
type StringHeader struct { Data uintptr // Data 指向底层的字符数组 Len int // Len 用来表示字符串的长度 }

字符串的本质上是一串字符数组,每个字符都在存储时对应了一个或多个整数。用这些整数来表示字符,比如打印 hello 的字节数组如下:

复制代码
1
2
3
4
5
6
7
8
9
package main import "fmt" func main() { var hello = "Hello" for i := 0; i < len(hello); i++ { fmt.Printf("%x ", hello[i]) } } // Output: 48 65 6c 6c 6f

字符串的底层原理

字符串有特殊标识,有两种声明方式:

var s1 string = `hello world`
var s2 string = "hello world"

字符串常量在词法解析阶段最终会被标记为 StringLit 类型的 Token 并被传递到编译的下一个阶段。
在语法分析阶段,采取递归下降的方式读取 UTF-8 字符,单撇号或双引号是字符串的标识。

分析的逻辑位于  syntax/scanner.go 文件中:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
func (s *scanner) stdString() { ok := true s.nextch() for { if s.ch == '"' { s.nextch() break } if s.ch == '\' { s.nextch() if !s.escape('"') { ok = false } continue } if s.ch == 'n' { s.errorf("newline in string") ok = false break } if s.ch < 0 { s.errorAtf(0, "string not terminated") ok = false break } s.nextch() } s.setLit(StringLit, ok) } func (s *scanner) rawString() { ok := true s.nextch() for { if s.ch == '`' { s.nextch() break } if s.ch < 0 { s.errorAtf(0, "string not terminated") ok = false break } s.nextch() } // We leave CRs in the string since they are part of the // literal (even though they are not part of the literal // value). s.setLit(StringLit, ok) }

从上面的代码可以看到,Go 中有两种字符串的检查:一种是标准字符串以双引号定义 "",如 "Hello,World", 还有一种是原始字符串,用  定义的, 因此针对两种字符串有两种语法分析函数:

  • 如果是单撇号,则调用 rawString 函数
  • 如果是双引号,则调用 stdString 函数

到此这篇关于深入理解 Go 中的字符串的文章就介绍到这了,更多相关Go 字符串内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是会撒娇花瓣最近收集整理的关于深入理解 Go 中的字符串的全部内容,更多相关深入理解 Go 中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部