深入理解 Golang 的字符串


Posted in Golang onMay 04, 2022

字符串的本质

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

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

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

>>> 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 中也一样:

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 语言的隐式声明,以字符 “\0” 作为终止符
  • 一种是 Go 语言的显式声明

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

type StringHeader struct {
Data uintptr // Data 指向底层的字符数组
Len int // Len 用来表示字符串的长度
}

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

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 文件中:

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 函数
Golang 相关文章推荐
go语言map与string的相互转换的实现
Apr 07 Golang
Go语言-为什么返回值为接口类型,却返回结构体
Apr 24 Golang
golang 如何通过反射创建新对象
Apr 28 Golang
golang import自定义包方式
Apr 29 Golang
解决Go gorm踩过的坑
Apr 30 Golang
golang 实现Location跳转方式
May 02 Golang
Goland使用Go Modules创建/管理项目的操作
May 06 Golang
Golang全局变量加锁的问题解决
May 08 Golang
GoLang中生成UUID唯一标识的实现
May 08 Golang
Go语言空白表示符_的实例用法
Jul 04 Golang
go使用Gin框架利用阿里云实现短信验证码功能
Aug 04 Golang
Go获取两个时区的时间差
Apr 20 Golang
Golang入门之计时器
May 04 #Golang
Golang 入门 之url 包
May 04 #Golang
Golang解析JSON对象
Apr 30 #Golang
Golang 并发编程 SingleFlight模式
Golang 实现 WebSockets 之创建 WebSockets
Apr 24 #Golang
Golang 实现WebSockets
Golang ort 中的sortInts 方法
Apr 24 #Golang
You might like
PHP学习笔记 (1) 环境配置与代码调试
2011/06/19 PHP
ThinkPHP CURD方法之data方法详解
2014/06/18 PHP
php获得文件大小和文件创建时间的方法
2015/03/13 PHP
Zend Framework框架实现类似Google搜索分页效果
2016/11/25 PHP
通过action传过来的值在option获取进行验证的方法
2013/11/14 Javascript
JavaScript设计模式之外观模式介绍
2014/12/28 Javascript
JS实现鼠标滑过折叠与展开菜单效果代码
2015/09/06 Javascript
mongoose中利用populate处理嵌套的方法
2017/05/26 Javascript
vue项目打包之后背景样式丢失的解决方案
2019/01/17 Javascript
vue项目添加多页面配置的步骤详解
2019/05/22 Javascript
jQuery zTree如何改变指定节点文本样式
2020/10/16 jQuery
JavaScript 如何在浏览器中使用摄像头
2020/12/02 Javascript
python在Windows8下获取本机ip地址的方法
2015/03/14 Python
Python中functools模块的常用函数解析
2016/06/30 Python
python3使用scrapy生成csv文件代码示例
2017/12/28 Python
python tkinter界面居中显示的方法
2018/10/11 Python
padas 生成excel 增加sheet表的实例
2018/12/11 Python
Python去除字符串前后空格的几种方法
2019/03/04 Python
在python shell中运行python文件的实现
2019/12/21 Python
python批量处理txt文件的实例代码
2020/01/13 Python
在线服装零售商:SheIn
2016/07/22 全球购物
为奢侈时尚带来了慈善元素:Olivela
2018/09/29 全球购物
英国标准协会商店:BSI Shop
2019/02/25 全球购物
新加坡网上美容店:Hermo新加坡
2019/06/19 全球购物
澳大利亚宠物食品和用品商店:PETstock
2020/01/02 全球购物
实习教师个人的自我评价
2013/11/08 职场文书
中层竞聘演讲稿
2014/01/09 职场文书
高中生期末评语
2014/01/28 职场文书
物理专业大学生职业生涯规划书
2014/02/07 职场文书
《金钱的魔力》教学反思
2014/02/24 职场文书
2014年预备党员学习两会心得体会
2014/03/17 职场文书
村干部四风问题整改措施
2014/09/30 职场文书
小班下学期个人总结
2015/02/12 职场文书
运动会报道稿大全
2015/07/23 职场文书
《为人民服务》教学反思
2016/02/20 职场文书
详解CSS不受控制的position fixed
2021/05/25 HTML / CSS