2022-08-04 06:59:33 +00:00
<!DOCTYPE HTML>
< html lang = "zh" class = "sidebar-visible no-js light" >
< head >
2024-08-08 07:47:03 +00:00
<!-- Book generated using https://github.com/wa - lang/mnbook -->
2022-08-04 06:59:33 +00:00
< meta charset = "UTF-8" >
< title > 字符串 - Go语言圣经< / title >
<!-- Custom HTML head -->
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
2024-08-08 07:47:03 +00:00
< meta name = "description" content = "" >
2022-08-04 06:59:33 +00:00
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< meta name = "theme-color" content = "#ffffff" / >
2024-08-08 07:47:03 +00:00
< link rel = "icon" href = "../favicon.svg" >
< link rel = "shortcut icon" href = "../favicon.png" >
< link rel = "stylesheet" href = "../static/mnbook/css/variables.css" >
< link rel = "stylesheet" href = "../static/mnbook/css/general.css" >
< link rel = "stylesheet" href = "../static/mnbook/css/chrome.css" >
< link rel = "stylesheet" href = "../static/mnbook/css/print.css" media = "print" >
2022-08-04 06:59:33 +00:00
<!-- Fonts -->
2024-08-08 07:47:03 +00:00
< link rel = "stylesheet" href = "../static/mnbook/FontAwesome/css/font-awesome.css" >
< link rel = "stylesheet" href = "../static/mnbook/fonts/fonts.css" >
2022-08-04 06:59:33 +00:00
<!-- Highlight.js Stylesheets -->
2024-08-08 07:47:03 +00:00
< link rel = "stylesheet" href = "../static/mnbook/highlight.css" >
< link rel = "stylesheet" href = "../static/mnbook/tomorrow-night.css" >
< link rel = "stylesheet" href = "../static/mnbook/ayu-highlight.css" >
2022-08-04 06:59:33 +00:00
<!-- Custom theme stylesheets -->
< / head >
< body >
<!-- Provide site root to javascript -->
< script type = "text/javascript" >
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
< / script >
<!-- Work around some values being stored in localStorage wrapped in quotes -->
< script type = "text/javascript" >
try {
2024-08-08 07:47:03 +00:00
var theme = localStorage.getItem('mnbook-theme');
var sidebar = localStorage.getItem('mnbook-sidebar');
2022-08-04 06:59:33 +00:00
if (theme.startsWith('"') & & theme.endsWith('"')) {
2024-08-08 07:47:03 +00:00
localStorage.setItem('mnbook-theme', theme.slice(1, theme.length - 1));
2022-08-04 06:59:33 +00:00
}
if (sidebar.startsWith('"') & & sidebar.endsWith('"')) {
2024-08-08 07:47:03 +00:00
localStorage.setItem('mnbook-sidebar', sidebar.slice(1, sidebar.length - 1));
2022-08-04 06:59:33 +00:00
}
} catch (e) { }
< / script >
<!-- Set the theme before any content is loaded, prevents flash -->
< script type = "text/javascript" >
var theme;
2024-08-08 07:47:03 +00:00
try { theme = localStorage.getItem('mnbook-theme'); } catch(e) { }
2022-08-04 06:59:33 +00:00
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('no-js')
html.classList.remove('light')
html.classList.add(theme);
html.classList.add('js');
< / script >
<!-- Hide / unhide sidebar before it is displayed -->
< script type = "text/javascript" >
var html = document.querySelector('html');
var sidebar = 'hidden';
if (document.body.clientWidth >= 1080) {
2024-08-08 07:47:03 +00:00
try { sidebar = localStorage.getItem('mnbook-sidebar'); } catch(e) { }
2022-08-04 06:59:33 +00:00
sidebar = sidebar || 'visible';
}
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
< / script >
< nav id = "sidebar" class = "sidebar" aria-label = "Table of contents" >
< div class = "sidebar-scrollbox" >
2024-08-08 07:47:03 +00:00
< ol class = "chapter" >
< li class = "chapter-item expanded " >
< a href = "../index.html" > Go语言圣经< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../preface-zh.html" > 译者序< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../preface.html" > 前言< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1.html" > < strong aria-hidden = "true" > 1.< / strong > 入门< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1-01.html" > < strong aria-hidden = "true" > 1.1.< / strong > Hello, World< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1-02.html" > < strong aria-hidden = "true" > 1.2.< / strong > 命令行参数< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1-03.html" > < strong aria-hidden = "true" > 1.3.< / strong > 查找重复的行< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1-04.html" > < strong aria-hidden = "true" > 1.4.< / strong > GIF动画< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1-05.html" > < strong aria-hidden = "true" > 1.5.< / strong > 获取URL< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1-06.html" > < strong aria-hidden = "true" > 1.6.< / strong > 并发获取多个URL< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1-07.html" > < strong aria-hidden = "true" > 1.7.< / strong > Web服务< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch1/ch1-08.html" > < strong aria-hidden = "true" > 1.8.< / strong > 本章要点< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch2/ch2.html" > < strong aria-hidden = "true" > 2.< / strong > 程序结构< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch2/ch2-01.html" > < strong aria-hidden = "true" > 2.1.< / strong > 命名< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch2/ch2-02.html" > < strong aria-hidden = "true" > 2.2.< / strong > 声明< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch2/ch2-03.html" > < strong aria-hidden = "true" > 2.3.< / strong > 变量< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch2/ch2-04.html" > < strong aria-hidden = "true" > 2.4.< / strong > 赋值< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch2/ch2-05.html" > < strong aria-hidden = "true" > 2.5.< / strong > 类型< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch2/ch2-06.html" > < strong aria-hidden = "true" > 2.6.< / strong > 包和文件< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch2/ch2-07.html" > < strong aria-hidden = "true" > 2.7.< / strong > 作用域< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch3/ch3.html" > < strong aria-hidden = "true" > 3.< / strong > 基础数据类型< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch3/ch3-01.html" > < strong aria-hidden = "true" > 3.1.< / strong > 整型< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch3/ch3-02.html" > < strong aria-hidden = "true" > 3.2.< / strong > 浮点数< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch3/ch3-03.html" > < strong aria-hidden = "true" > 3.3.< / strong > 复数< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch3/ch3-04.html" > < strong aria-hidden = "true" > 3.4.< / strong > 布尔型< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch3/ch3-05.html" class = "active" > < strong aria-hidden = "true" > 3.5.< / strong > 字符串< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch3/ch3-06.html" > < strong aria-hidden = "true" > 3.6.< / strong > 常量< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch4/ch4.html" > < strong aria-hidden = "true" > 4.< / strong > 复合数据类型< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch4/ch4-01.html" > < strong aria-hidden = "true" > 4.1.< / strong > 数组< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch4/ch4-02.html" > < strong aria-hidden = "true" > 4.2.< / strong > Slice< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch4/ch4-03.html" > < strong aria-hidden = "true" > 4.3.< / strong > Map< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch4/ch4-04.html" > < strong aria-hidden = "true" > 4.4.< / strong > 结构体< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch4/ch4-05.html" > < strong aria-hidden = "true" > 4.5.< / strong > JSON< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch4/ch4-06.html" > < strong aria-hidden = "true" > 4.6.< / strong > 文本和HTML模板< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5.html" > < strong aria-hidden = "true" > 5.< / strong > 函数< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-01.html" > < strong aria-hidden = "true" > 5.1.< / strong > 函数声明< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-02.html" > < strong aria-hidden = "true" > 5.2.< / strong > 递归< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-03.html" > < strong aria-hidden = "true" > 5.3.< / strong > 多返回值< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-04.html" > < strong aria-hidden = "true" > 5.4.< / strong > 错误< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-05.html" > < strong aria-hidden = "true" > 5.5.< / strong > 函数值< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-06.html" > < strong aria-hidden = "true" > 5.6.< / strong > 匿名函数< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-07.html" > < strong aria-hidden = "true" > 5.7.< / strong > 可变参数< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-08.html" > < strong aria-hidden = "true" > 5.8.< / strong > Deferred函数< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-09.html" > < strong aria-hidden = "true" > 5.9.< / strong > Panic异常< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch5/ch5-10.html" > < strong aria-hidden = "true" > 5.10.< / strong > Recover捕获异常< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch6/ch6.html" > < strong aria-hidden = "true" > 6.< / strong > 方法< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch6/ch6-01.html" > < strong aria-hidden = "true" > 6.1.< / strong > 方法声明< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch6/ch6-02.html" > < strong aria-hidden = "true" > 6.2.< / strong > 基于指针对象的方法< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch6/ch6-03.html" > < strong aria-hidden = "true" > 6.3.< / strong > 通过嵌入结构体来扩展类型< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch6/ch6-04.html" > < strong aria-hidden = "true" > 6.4.< / strong > 方法值和方法表达式< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch6/ch6-05.html" > < strong aria-hidden = "true" > 6.5.< / strong > 示例: Bit数组< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch6/ch6-06.html" > < strong aria-hidden = "true" > 6.6.< / strong > 封装< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7.html" > < strong aria-hidden = "true" > 7.< / strong > 接口< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-01.html" > < strong aria-hidden = "true" > 7.1.< / strong > 接口是合约< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-02.html" > < strong aria-hidden = "true" > 7.2.< / strong > 接口类型< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-03.html" > < strong aria-hidden = "true" > 7.3.< / strong > 实现接口的条件< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-04.html" > < strong aria-hidden = "true" > 7.4.< / strong > flag.Value接口< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-05.html" > < strong aria-hidden = "true" > 7.5.< / strong > 接口值< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-06.html" > < strong aria-hidden = "true" > 7.6.< / strong > sort.Interface接口< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-07.html" > < strong aria-hidden = "true" > 7.7.< / strong > http.Handler接口< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-08.html" > < strong aria-hidden = "true" > 7.8.< / strong > error接口< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-09.html" > < strong aria-hidden = "true" > 7.9.< / strong > 示例: 表达式求值< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-10.html" > < strong aria-hidden = "true" > 7.10.< / strong > 类型断言< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-11.html" > < strong aria-hidden = "true" > 7.11.< / strong > 基于类型断言识别错误类型< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-12.html" > < strong aria-hidden = "true" > 7.12.< / strong > 通过类型断言查询接口< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-13.html" > < strong aria-hidden = "true" > 7.13.< / strong > 类型分支< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-14.html" > < strong aria-hidden = "true" > 7.14.< / strong > 示例: 基于标记的XML解码< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch7/ch7-15.html" > < strong aria-hidden = "true" > 7.15.< / strong > 补充几点< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8.html" > < strong aria-hidden = "true" > 8.< / strong > Goroutines和Channels< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-01.html" > < strong aria-hidden = "true" > 8.1.< / strong > Goroutines< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-02.html" > < strong aria-hidden = "true" > 8.2.< / strong > 示例: 并发的Clock服务< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-03.html" > < strong aria-hidden = "true" > 8.3.< / strong > 示例: 并发的Echo服务< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-04.html" > < strong aria-hidden = "true" > 8.4.< / strong > Channels< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-05.html" > < strong aria-hidden = "true" > 8.5.< / strong > 并发的循环< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-06.html" > < strong aria-hidden = "true" > 8.6.< / strong > 示例: 并发的Web爬虫< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-07.html" > < strong aria-hidden = "true" > 8.7.< / strong > 基于select的多路复用< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-08.html" > < strong aria-hidden = "true" > 8.8.< / strong > 示例: 并发的目录遍历< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-09.html" > < strong aria-hidden = "true" > 8.9.< / strong > 并发的退出< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch8/ch8-10.html" > < strong aria-hidden = "true" > 8.10.< / strong > 示例: 聊天服务< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9.html" > < strong aria-hidden = "true" > 9.< / strong > 基于共享变量的并发< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9-01.html" > < strong aria-hidden = "true" > 9.1.< / strong > 竞争条件< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9-02.html" > < strong aria-hidden = "true" > 9.2.< / strong > sync.Mutex互斥锁< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9-03.html" > < strong aria-hidden = "true" > 9.3.< / strong > sync.RWMutex读写锁< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9-04.html" > < strong aria-hidden = "true" > 9.4.< / strong > 内存同步< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9-05.html" > < strong aria-hidden = "true" > 9.5.< / strong > sync.Once惰性初始化< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9-06.html" > < strong aria-hidden = "true" > 9.6.< / strong > 竞争条件检测< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9-07.html" > < strong aria-hidden = "true" > 9.7.< / strong > 示例: 并发的非阻塞缓存< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch9/ch9-08.html" > < strong aria-hidden = "true" > 9.8.< / strong > Goroutines和线程< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch10/ch10.html" > < strong aria-hidden = "true" > 10.< / strong > 包和工具< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch10/ch10-01.html" > < strong aria-hidden = "true" > 10.1.< / strong > 包简介< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch10/ch10-02.html" > < strong aria-hidden = "true" > 10.2.< / strong > 导入路径< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch10/ch10-03.html" > < strong aria-hidden = "true" > 10.3.< / strong > 包声明< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch10/ch10-04.html" > < strong aria-hidden = "true" > 10.4.< / strong > 导入声明< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch10/ch10-05.html" > < strong aria-hidden = "true" > 10.5.< / strong > 包的匿名导入< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch10/ch10-06.html" > < strong aria-hidden = "true" > 10.6.< / strong > 包和命名< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch10/ch10-07.html" > < strong aria-hidden = "true" > 10.7.< / strong > 工具< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch11/ch11.html" > < strong aria-hidden = "true" > 11.< / strong > 测试< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch11/ch11-01.html" > < strong aria-hidden = "true" > 11.1.< / strong > go test< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch11/ch11-02.html" > < strong aria-hidden = "true" > 11.2.< / strong > 测试函数< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch11/ch11-03.html" > < strong aria-hidden = "true" > 11.3.< / strong > 测试覆盖率< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch11/ch11-04.html" > < strong aria-hidden = "true" > 11.4.< / strong > 基准测试< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch11/ch11-05.html" > < strong aria-hidden = "true" > 11.5.< / strong > 剖析< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch11/ch11-06.html" > < strong aria-hidden = "true" > 11.6.< / strong > 示例函数< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12.html" > < strong aria-hidden = "true" > 12.< / strong > 反射< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-01.html" > < strong aria-hidden = "true" > 12.1.< / strong > 为何需要反射?< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-02.html" > < strong aria-hidden = "true" > 12.2.< / strong > reflect.Type和reflect.Value< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-03.html" > < strong aria-hidden = "true" > 12.3.< / strong > Display递归打印< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-04.html" > < strong aria-hidden = "true" > 12.4.< / strong > 示例: 编码S表达式< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-05.html" > < strong aria-hidden = "true" > 12.5.< / strong > 通过reflect.Value修改值< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-06.html" > < strong aria-hidden = "true" > 12.6.< / strong > 示例: 解码S表达式< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-07.html" > < strong aria-hidden = "true" > 12.7.< / strong > 获取结构体字段标签< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-08.html" > < strong aria-hidden = "true" > 12.8.< / strong > 显示一个类型的方法集< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch12/ch12-09.html" > < strong aria-hidden = "true" > 12.9.< / strong > 几点忠告< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../ch13/ch13.html" > < strong aria-hidden = "true" > 13.< / strong > 底层编程< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../ch13/ch13-01.html" > < strong aria-hidden = "true" > 13.1.< / strong > unsafe.Sizeof, Alignof 和 Offsetof< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch13/ch13-02.html" > < strong aria-hidden = "true" > 13.2.< / strong > unsafe.Pointer< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch13/ch13-03.html" > < strong aria-hidden = "true" > 13.3.< / strong > 示例: 深度相等判断< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch13/ch13-04.html" > < strong aria-hidden = "true" > 13.4.< / strong > 通过cgo调用C代码< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../ch13/ch13-05.html" > < strong aria-hidden = "true" > 13.5.< / strong > 几点忠告< / a >
< / li >
< / ol >
< li class = "chapter-item expanded " >
< a href = "../appendix/appendix.html" > < strong aria-hidden = "true" > 14.< / strong > 附录< / a >
< / li >
< ol class = "section" >
< li class = "chapter-item expanded " >
< a href = "../appendix/appendix-a-errata.html" > < strong aria-hidden = "true" > 14.1.< / strong > 附录A: 原文勘误< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../appendix/appendix-b-author.html" > < strong aria-hidden = "true" > 14.2.< / strong > 附录B: 作者译者< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../appendix/appendix-c-cpoyright.html" > < strong aria-hidden = "true" > 14.3.< / strong > 附录C: 译文授权< / a >
< / li >
< li class = "chapter-item expanded " >
< a href = "../appendix/appendix-d-translations.html" > < strong aria-hidden = "true" > 14.4.< / strong > 附录D: 其它语言< / a >
< / li >
< / ol >
< / ol >
< / div >
2022-08-04 06:59:33 +00:00
< div id = "sidebar-resize-handle" class = "sidebar-resize-handle" > < / div >
< / nav >
< div id = "page-wrapper" class = "page-wrapper" >
< div class = "page" >
< div id = "menu-bar-hover-placeholder" > < / div >
< div id = "menu-bar" class = "menu-bar sticky bordered" >
< div class = "left-buttons" >
< button id = "sidebar-toggle" class = "icon-button" type = "button" title = "Toggle Table of Contents" aria-label = "Toggle Table of Contents" aria-controls = "sidebar" >
< i class = "fa fa-bars" > < / i >
< / button >
< button id = "theme-toggle" class = "icon-button" type = "button" title = "Change theme" aria-label = "Change theme" aria-haspopup = "true" aria-expanded = "false" aria-controls = "theme-list" >
< i class = "fa fa-paint-brush" > < / i >
< / button >
< ul id = "theme-list" class = "theme-popup" aria-label = "Themes" role = "menu" >
< li role = "none" > < button role = "menuitem" class = "theme" id = "light" > Light (default)< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "coal" > Coal< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "navy" > Navy< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "ayu" > Ayu< / button > < / li >
< / ul >
2024-08-08 07:47:03 +00:00
< / div >
2022-08-04 06:59:33 +00:00
2024-08-08 07:47:03 +00:00
< h1 class = "menu-title" > < a href = "../index.html" > Go语言圣经< / a > < / h1 >
2022-08-04 06:59:33 +00:00
< div class = "right-buttons" >
2024-08-08 07:47:03 +00:00
< a href = "https://github.com/gopl-zh/gopl-zh.github.com" title = "Git repository" aria-label = "Git repository" >
2022-08-04 06:59:33 +00:00
< i id = "git-repository-button" class = "fa fa-github" > < / i >
< / a >
2024-08-08 07:47:03 +00:00
< a href = "https://github.com/gopl-zh/gopl-zh.github.com/edit/master/ch3/ch3-05.md" title = "Suggest an edit" aria-label = "Suggest an edit" >
2022-08-04 06:59:33 +00:00
< i id = "git-edit-button" class = "fa fa-edit" > < / i >
< / a >
< / div >
< / div >
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
< script type = "text/javascript" >
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
< / script >
< div id = "content" class = "content" >
<!-- Page table of contents -->
< div class = "sidetoc" > < nav class = "pagetoc" > < / nav > < / div >
2024-08-08 07:47:03 +00:00
2022-08-04 06:59:33 +00:00
< main >
2024-08-08 07:47:03 +00:00
< ul dir = "auto" > < li > < em > 凹语言(Go实现, 面向WASM设计): < a href = "https://github.com/wa-lang/wa" > https://github.com/wa-lang/wa< / a > < / em > < / li > < li > < em > MnBook(Go语言实现的MD电子书构建工具): < a href = "https://github.com/wa-lang/mnbook" > https://github.com/wa-lang/mnbook< / a > < / em > < / li > < / ul > < hr >
2022-08-04 06:59:33 +00:00
2024-08-08 07:47:03 +00:00
< h2 > 3.5. 字符串< / h2 >
2022-08-04 06:59:33 +00:00
< p > 一个字符串是一个不可改变的字节序列。字符串可以包含任意的数据, 包括byte值0, 但是通常是用来包含人类可读的文本。文本字符串通常被解释为采用UTF8编码的Unicode码点( rune) 序列, 我们稍后会详细讨论这个问题。< / p >
< p > 内置的len函数可以返回一个字符串中的字节数目( 不是rune字符数目) , 索引操作s[i]返回第i个字节的字节值, i必须满足0 ≤ i< len(s)条件约束。< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > s := " hello, world"
fmt.Println(len(s)) // " 12"
fmt.Println(s[0], s[7]) // " 104 119" ('h' and 'w')
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 如果试图访问超出字符串索引范围的字节将会导致panic异常: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > c := s[len(s)] // panic: index out of range
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 第i个字节并不一定是字符串的第i个字符, 因为对于非ASCII字符的UTF8编码会要两个或多个字节。我们先简单说下字符的工作方式。< / p >
< p > 子字符串操作s[i:j]基于原始的s字符串的第i个字节开始到第j个字节( 并不包含j本身) 生成一个新字符串。生成的新字符串将包含j-i个字节。< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(s[0:5]) // " hello"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 同样, 如果索引超出字符串范围或者j小于i的话将导致panic异常。< / p >
< p > 不管i还是j都可能被忽略, 当它们被忽略时将采用0作为开始位置, 采用len(s)作为结束的位置。< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(s[:5]) // " hello"
fmt.Println(s[7:]) // " world"
fmt.Println(s[:]) // " hello, world"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 其中+操作符将两个字符串连接构造一个新字符串:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(" goodbye" + s[5:]) // " goodbye, world"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 字符串可以用==和< 进行比较;比较通过逐个字节比较完成的,因此比较的结果是字符串自然编码的顺序。< / p >
< p > 字符串的值是不可变的:一个字符串包含的字节序列永远不会被改变,当然我们也可以给一个字符串变量分配一个新字符串值。可以像下面这样将一个字符串追加到另一个字符串:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > s := " left foot"
t := s
s += " , right foot"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 这并不会导致原始的字符串值被改变, 但是变量s将因为+=语句持有一个新的字符串值, 但是t依然是包含原先的字符串值。< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(s) // " left foot, right foot"
fmt.Println(t) // " left foot"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 因为字符串是不可修改的,因此尝试修改字符串内部数据的操作也是被禁止的:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > s[0] = 'L' // compile error: cannot assign to s[0]
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 不变性意味着如果两个字符串共享相同的底层数据的话也是安全的, 这使得复制任何长度的字符串代价是低廉的。同样, 一个字符串s和对应的子字符串切片s[7:]的操作也可以安全地共享相同的内存,因此字符串切片操作代价也是低廉的。在这两种情况下都没有必要分配新的内存。 图3.4演示了一个字符串和两个子串共享相同的底层数据。< / p >
2024-08-08 07:47:03 +00:00
< h3 > 3.5.1. 字符串面值< / h3 >
2022-08-04 06:59:33 +00:00
< p > 字符串值也可以用字符串面值方式编写,只要将一系列字节序列包含在双引号内即可:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code > " Hello, world"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
2024-08-08 07:47:03 +00:00
< p > < img src = "../images/ch3-04.png" alt = "" > < / p >
2022-08-04 06:59:33 +00:00
< p > 因为Go语言源文件总是用UTF8编码, 并且Go语言的文本字符串也以UTF8编码的方式处理, 因此我们可以将Unicode码点也写到字符串面值中。< / p >
< p > 在一个双引号包含的字符串面值中,可以用以反斜杠< code > \< / code > 开头的转义序列插入任意的数据。下面的换行、回车和制表符等是常见的ASCII控制代码的转义方式: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code > \a 响铃
\b 退格
\f 换页
\n 换行
\r 回车
\t 制表符
\v 垂直制表符
\' 单引号(只用在 '\'' 形式的rune符号面值中)
\" 双引号(只用在 " ..." 形式的字符串面值中)
\\ 反斜杠
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 可以通过十六进制或八进制转义在字符串面值中包含任意的字节。一个十六进制的转义形式是< code > \xhh< / code > , 其中两个h表示十六进制数字( 大写或小写都可以) 。一个八进制转义形式是< code > \ooo< / code > , 包含三个八进制的o数字( 0到7) , 但是不能超过< code > \377< / code > ( 译注: 对应一个字节的范围, 十进制为255) 。每一个单一的字节表达一个特定的值。稍后我们将看到如何将一个Unicode码点写到字符串面值中。< / p >
< p > 一个原生的字符串面值形式是`...`,使用反引号代替双引号。在原生的字符串面值中,没有转义操作;全部的内容都是字面的意思,包含退格和换行,因此一个程序中的原生字符串面值可能跨越多行(译注:在原生字符串面值内部是无法直接写`字符的,可以用八进制或十六进制转义或+" `" 连接字符串常量完成) 。唯一的特殊处理是会删除回车以保证在所有平台上的值都是一样的, 包括那些把回车也放入文本文件的系统( 译注: Windows系统会把回车和换行一起放入文本文件中) 。< / p >
< p > 原生字符串面值用于编写正则表达式会很方便, 因为正则表达式往往会包含很多反斜杠。原生字符串面值同时被广泛应用于HTML模板、JSON面值、命令行提示信息以及那些需要扩展到多行的场景。< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > const GoUsage = `Go is a tool for managing Go source code.
Usage:
go command [arguments]
...`
2022-08-04 06:59:33 +00:00
< / code > < / pre >
2024-08-08 07:47:03 +00:00
< h3 > 3.5.2. Unicode< / h3 >
2022-08-04 06:59:33 +00:00
< p > 在很久以前, 世界还是比较简单的, 起码计算机世界就只有一个ASCII字符集: 美国信息交换标准代码。ASCII, 更准确地说是美国的ASCII, 使用7bit来表示128个字符: 包含英文字母的大小写、数字、各种标点符号和设备控制符。对于早期的计算机程序来说, 这些就足够了, 但是这也导致了世界上很多其他地区的用户无法直接使用自己的符号系统。随着互联网的发展, 混合多种语言的数据变得很常见( 译注: 比如本身的英文原文或中文翻译都包含了ASCII、中文、日文等多种语言字符) 。如何有效处理这些包含了各种语言的丰富多样的文本数据呢? < / p >
2024-08-08 07:47:03 +00:00
< p > 答案就是使用Unicode( < a href = "http://unicode.org" > http://unicode.org< / a > ) , 它收集了这个世界上所有的符号系统, 包括重音符号和其它变音符号, 制表符和回车符, 还有很多神秘的符号, 每个符号都分配一个唯一的Unicode码点, Unicode码点对应Go语言中的rune整数类型( 译注: rune是int32等价类型) 。< / p >
2022-08-04 06:59:33 +00:00
< p > 在第八版本的Unicode标准里收集了超过120,000个字符, 涵盖超过100多种语言。这些在计算机程序和数据中是如何体现的呢? 通用的表示一个Unicode码点的数据类型是int32, 也就是Go语言中rune对应的类型; 它的同义词rune符文正是这个意思。< / p >
< p > 我们可以将一个符文序列表示为一个int32序列。这种编码方式叫UTF-32或UCS-4, 每个Unicode码点都使用同样大小的32bit来表示。这种方式比较简单统一, 但是它会浪费很多存储空间, 因为大多数计算机可读的文本是ASCII字符, 本来每个ASCII字符只需要8bit或1字节就能表示。而且即使是常用的字符也远少于65,536个, 也就是说用16bit编码方式就能表达常用字符。但是, 还有其它更好的编码方法吗? < / p >
2024-08-08 07:47:03 +00:00
< h3 > 3.5.3. UTF-8< / h3 >
2022-08-04 06:59:33 +00:00
< p > UTF8是一个将Unicode码点编码为字节序列的变长编码。UTF8编码是由Go语言之父Ken Thompson和Rob Pike共同发明的, 现在已经是Unicode的标准。UTF8编码使用1到4个字节来表示每个Unicode码点, ASCII部分字符只使用1个字节, 常用字符部分使用2或3个字节表示。每个符号编码后第一个字节的高端bit位用于表示编码总共有多少个字节。如果第一个字节的高端bit为0, 则表示对应7bit的ASCII字符, ASCII字符每个字符依然是一个字节, 和传统的ASCII编码兼容。如果第一个字节的高端bit是110, 则说明需要2个字节; 后续的每个高端bit都以10开头。更大的Unicode码点也是采用类似的策略处理。< / p >
2024-08-08 07:47:03 +00:00
< pre > < code > 0xxxxxxx runes 0-127 (ASCII)
110xxxxx 10xxxxxx 128-2047 (values < 128 unused)
1110xxxx 10xxxxxx 10xxxxxx 2048-65535 (values < 2048 unused)
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 65536-0x10ffff (other values unused)
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 变长的编码无法直接通过索引来访问第n个字符, 但是UTF8编码获得了很多额外的优点。首先UTF8编码比较紧凑, 完全兼容ASCII码, 并且可以自动同步: 它可以通过向前回朔最多3个字节就能确定当前字符编码的开始字节的位置。它也是一个前缀编码, 所以当从左向右解码时不会有任何歧义也并不需要向前查看( 译注: 像GBK之类的编码, 如果不知道起点位置则可能会出现歧义) 。没有任何字符的编码是其它字符编码的子串, 或是其它编码序列的字串, 因此搜索一个字符时只要搜索它的字节编码序列即可, 不用担心前后的上下文会对搜索结果产生干扰。同时UTF8编码的顺序和Unicode码点的顺序一致, 因此可以直接排序UTF8编码序列。同时因为没有嵌入的NUL(0)字节, 可以很好地兼容那些使用NUL作为字符串结尾的编程语言。< / p >
< p > Go语言的源文件采用UTF8编码, 并且Go语言处理UTF8编码的文本也很出色。unicode包提供了诸多处理rune字符相关功能的函数( 比如区分字母和数字, 或者是字母的大写和小写转换等) , unicode/utf8包则提供了用于rune字符序列的UTF8编码和解码的功能。< / p >
< p > 有很多Unicode字符很难直接从键盘输入, 并且还有很多字符有着相似的结构; 有一些甚至是不可见的字符( 译注: 中文和日文就有很多相似但不同的字) 。Go语言字符串面值中的Unicode转义字符让我们可以通过Unicode码点输入特殊的字符。有两种形式: < code > \uhhhh< / code > 对应16bit的码点值, < code > \Uhhhhhhhh< / code > 对应32bit的码点值, 其中h是一个十六进制数字; 一般很少需要使用32bit的形式。每一个对应码点的UTF8编码。例如: 下面的字母串面值都表示相同的值: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code > " 世界"
" \xe4\xb8\x96\xe7\x95\x8c"
" \u4e16\u754c"
" \U00004e16\U0000754c"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 上面三个转义序列都为第一个字符串提供替代写法,但是它们的值都是相同的。< / p >
< p > Unicode转义也可以使用在rune字符中。下面三个字符是等价的: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code > '世' '\u4e16' '\U00004e16'
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 对于小于256的码点值可以写在一个十六进制转义字节中, 例如< code > \x41< / code > 对应字符'A',但是对于更大的码点则必须使用< code > \u< / code > 或< code > \U< / code > 转义形式。因此,< code > \xe4\xb8\x96< / code > 并不是一个合法的rune字符, 虽然这三个字节对应一个有效的UTF8编码的码点。< / p >
< p > 得益于UTF8编码优良的设计, 诸多字符串操作都不需要解码操作。我们可以不用解码直接测试一个字符串是否是另一个字符串的前缀: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > func HasPrefix(s, prefix string) bool {
return len(s) > = len(prefix) & & s[:len(prefix)] == prefix
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 或者是后缀测试:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > func HasSuffix(s, suffix string) bool {
return len(s) > = len(suffix) & & s[len(s)-len(suffix):] == suffix
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 或者是包含子串测试:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > func Contains(s, substr string) bool {
for i := 0; i < len(s); i++ {
if HasPrefix(s[i:], substr) {
return true
}
}
return false
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 对于UTF8编码后文本的处理和原始的字节处理逻辑是一样的。但是对应很多其它编码则并不是这样的。( 上面的函数都来自strings字符串处理包, 真实的代码包含了一个用哈希技术优化的Contains 实现。)< / p >
< p > 另一方面, 如果我们真的关心每个Unicode字符, 我们可以使用其它处理方式。考虑前面的第一个例子中的字符串, 它混合了中西两种字符。图3.5展示了它的内存表示形式。字符串包含13个字节, 以UTF8形式编码, 但是只对应9个Unicode字符: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > import " unicode/utf8"
s := " Hello, 世界"
fmt.Println(len(s)) // " 13"
fmt.Println(utf8.RuneCountInString(s)) // " 9"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 为了处理这些真实的字符, 我们需要一个UTF8解码器。unicode/utf8包提供了该功能, 我们可以这样使用: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
fmt.Printf(" %d\t%c\n" , i, r)
i += size
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 每一次调用DecodeRuneInString函数都返回一个r和长度, r对应字符本身, 长度对应r采用UTF8编码后的编码字节数目。长度可以用于更新第i个字符在字符串中的字节索引位置。但是这种编码方式是笨拙的, 我们需要更简洁的语法。幸运的是, Go语言的range循环在处理字符串的时候, 会自动隐式解码UTF8字符串。下面的循环运行如图3.5所示; 需要注意的是对于非ASCII, 索引更新的步长将超过1个字节。< / p >
2024-08-08 07:47:03 +00:00
< p > < img src = "../images/ch3-05.png" alt = "" > < / p >
< pre > < code class = "language-Go" > for i, r := range " Hello, 世界" {
fmt.Printf(" %d\t%q\t%d\n" , i, r, r)
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 我们可以使用一个简单的循环来统计字符串中字符的数目,像这样:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > n := 0
for _, _ = range s {
n++
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 像其它形式的循环那样,我们也可以忽略不需要的变量:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > n := 0
for range s {
n++
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 或者我们可以直接调用utf8.RuneCountInString(s)函数。< / p >
< p > 正如我们前面提到的, 文本字符串采用UTF8编码只是一种惯例, 但是对于循环的真正字符串并不是一个惯例, 这是正确的。如果用于循环的字符串只是一个普通的二进制数据, 或者是含有错误编码的UTF8数据, 将会发生什么呢? < / p >
< p > 每一个UTF8字符解码, 不管是显式地调用utf8.DecodeRuneInString解码或是在range循环中隐式地解码, 如果遇到一个错误的UTF8编码输入, 将生成一个特别的Unicode字符< code > \uFFFD< / code > ,在印刷中这个符号通常是一个黑色六角或钻石形状,里面包含一个白色的问号" ?" 。当程序遇到这样的一个字符, 通常是一个危险信号, 说明输入并不是一个完美没有错误的UTF8字符串。< / p >
< p > UTF8字符串作为交换格式是非常方便的, 但是在程序内部采用rune序列可能更方便, 因为rune大小一致, 支持数组索引和方便切割。< / p >
< p > 将[]rune类型转换应用到UTF8编码的字符串, 将返回字符串编码的Unicode码点序列: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > // " program" in Japanese katakana
s := " プログラム"
fmt.Printf(" % x\n" , s) // " e3 83 97 e3 83 ad e3 82 b0 e3 83 a9 e3 83 a0"
r := []rune(s)
fmt.Printf(" %x\n" , r) // " [30d7 30ed 30b0 30e9 30e0]"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > ( 在第一个Printf中的< code > % x< / code > 参数用于在每个十六进制数字前插入一个空格。)< / p >
< p > 如果是将一个[]rune类型的Unicode字符slice或数组转为string, 则对它们进行UTF8编码: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(string(r)) // " プログラム"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 将一个整数转型为字符串意思是生成以只包含对应Unicode码点字符的UTF8字符串: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(string(65)) // " A" , not " 65"
fmt.Println(string(0x4eac)) // " 京"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 如果对应码点的字符是无效的,则用< code > \uFFFD< / code > 无效字符作为替换:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(string(1234567)) // " ?"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
2024-08-08 07:47:03 +00:00
< h3 > 3.5.4. 字符串和Byte切片< / h3 >
2022-08-04 06:59:33 +00:00
< p > 标准库中有四个包对字符串处理尤为重要: bytes、strings、strconv和unicode包。strings包提供了许多如字符串的查询、替换、比较、截断、拆分和合并等功能。< / p >
< p > bytes包也提供了很多类似功能的函数, 但是针对和字符串有着相同结构的[]byte类型。因为字符串是只读的, 因此逐步构建字符串会导致很多分配和复制。在这种情况下, 使用bytes.Buffer类型将会更有效, 稍后我们将展示。< / p >
< p > strconv包提供了布尔型、整型数、浮点数和对应字符串的相互转换, 还提供了双引号转义相关的转换。< / p >
< p > unicode包提供了IsDigit、IsLetter、IsUpper和IsLower等类似功能, 它们用于给字符分类。每个函数有一个单一的rune类型的参数, 然后返回一个布尔值。而像ToUpper和ToLower之类的转换函数将用于rune字符的大小写转换。所有的这些函数都是遵循Unicode标准定义的字母、数字等分类规范。strings包也有类似的函数, 它们是ToUpper和ToLower, 将原始字符串的每个字符都做相应的转换, 然后返回新的字符串。< / p >
< p > 下面例子的basename函数灵感源于Unix shell的同名工具。在我们实现的版本中, basename(s)将看起来像是系统路径的前缀删除,同时将看似文件类型的后缀名部分删除:< / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(basename(" a/b/c.go" )) // " c"
fmt.Println(basename(" c.d.go" )) // " c.d"
fmt.Println(basename(" abc" )) // " abc"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 第一个版本并没有使用任何库,全部手工硬编码实现:< / p >
2024-08-08 07:47:03 +00:00
< p > <!-- raw HTML omitted --> <!-- raw HTML omitted --> gopl.io/ch3/basename1<!-- raw HTML omitted --> <!-- raw HTML omitted --> < / p >
< pre > < code class = "language-Go" > // basename removes directory components and a .suffix.
// e.g., a => a, a.go => a, a/b/c.go => c, a/b.c.go => b.c
func basename(s string) string {
// Discard last '/' and everything before.
for i := len(s) - 1; i > = 0; i-- {
if s[i] == '/' {
s = s[i+1:]
break
}
}
// Preserve everything before last '.'.
for i := len(s) - 1; i > = 0; i-- {
if s[i] == '.' {
s = s[:i]
break
}
}
return s
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 这个简化版本使用了strings.LastIndex库函数: < / p >
2024-08-08 07:47:03 +00:00
< p > <!-- raw HTML omitted --> <!-- raw HTML omitted --> gopl.io/ch3/basename2<!-- raw HTML omitted --> <!-- raw HTML omitted --> < / p >
< pre > < code class = "language-Go" > func basename(s string) string {
slash := strings.LastIndex(s, " /" ) // -1 if " /" not found
s = s[slash+1:]
if dot := strings.LastIndex(s, " ." ); dot > = 0 {
s = s[:dot]
}
return s
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > path和path/filepath包提供了关于文件路径名更一般的函数操作。使用斜杠分隔路径可以在任何操作系统上工作。斜杠本身不应该用于文件名, 但是在其他一些领域可能会用于文件名, 例如URL路径组件。相比之下, path/filepath包则使用操作系统本身的路径规则, 例如POSIX系统使用/foo/bar, 而Microsoft Windows使用< code > c:\foo\bar< / code > 等。< / p >
< p > 让我们继续另一个字符串的例子。函数的功能是将一个表示整数值的字符串, 每隔三个字符插入一个逗号分隔符, 例如“12345”处理后成为“12,345”。这个版本只适用于整数类型; 支持浮点数类型的留作练习。< / p >
2024-08-08 07:47:03 +00:00
< p > <!-- raw HTML omitted --> <!-- raw HTML omitted --> gopl.io/ch3/comma<!-- raw HTML omitted --> <!-- raw HTML omitted --> < / p >
< pre > < code class = "language-Go" > // comma inserts commas in a non-negative decimal integer string.
func comma(s string) string {
n := len(s)
if n < = 3 {
return s
}
return comma(s[:n-3]) + " ," + s[n-3:]
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 输入comma函数的参数是一个字符串。如果输入字符串的长度小于或等于3的话, 则不需要插入逗号分隔符。否则, comma函数将在最后三个字符前的位置将字符串切割为两个子串并插入逗号分隔符, 然后通过递归调用自身来得出前面的子串。< / p >
< p > 一个字符串是包含只读字节的数组, 一旦创建, 是不可变的。相比之下, 一个字节slice的元素则可以自由地修改。< / p >
< p > 字符串和字节slice之间可以相互转换: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > s := " abc"
b := []byte(s)
s2 := string(b)
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 从概念上讲,一个[]byte(s)转换是分配了一个新的字节数组用于保存字符串数据的拷贝, 然后引用这个底层的字节数组。编译器的优化可以避免在一些场景下分配和复制字符串数据, 但总的来说需要确保在变量b被修改的情况下, 原始的s字符串也不会改变。将一个字节slice转换到字符串的string(b)操作则是构造一个字符串拷贝, 以确保s2字符串是只读的。< / p >
< p > 为了避免转换中不必要的内存分配, bytes包和strings同时提供了许多实用函数。下面是strings包中的六个函数: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > func Contains(s, substr string) bool
func Count(s, sep string) int
func Fields(s string) []string
func HasPrefix(s, prefix string) bool
func Index(s, sep string) int
func Join(a []string, sep string) string
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > bytes包中也对应的六个函数: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > func Contains(b, subslice []byte) bool
func Count(s, sep []byte) int
func Fields(s []byte) [][]byte
func HasPrefix(s, prefix []byte) bool
func Index(s, sep []byte) int
func Join(s [][]byte, sep []byte) []byte
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 它们之间唯一的区别是字符串类型参数被替换成了字节slice类型的参数。< / p >
< p > bytes包还提供了Buffer类型用于字节slice的缓存。一个Buffer开始是空的, 但是随着string、byte或[]byte等类型数据的写入可以动态增长, 一个bytes.Buffer变量并不需要初始化, 因为零值也是有效的: < / p >
2024-08-08 07:47:03 +00:00
< p > <!-- raw HTML omitted --> <!-- raw HTML omitted --> gopl.io/ch3/printints<!-- raw HTML omitted --> <!-- raw HTML omitted --> < / p >
< pre > < code class = "language-Go" > // intsToString is like fmt.Sprint(values) but adds commas.
func intsToString(values []int) string {
var buf bytes.Buffer
buf.WriteByte('[')
for i, v := range values {
if i > 0 {
buf.WriteString(" , " )
}
fmt.Fprintf(& buf, " %d" , v)
}
buf.WriteByte(']')
return buf.String()
}
func main() {
fmt.Println(intsToString([]int{1, 2, 3})) // " [1, 2, 3]"
}
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 当向bytes.Buffer添加任意字符的UTF8编码时, 最好使用bytes.Buffer的WriteRune方法, 但是WriteByte方法对于写入类似'['和']'等ASCII字符则会更加有效。< / p >
< p > bytes.Buffer类型有着很多实用的功能, 我们在第七章讨论接口时将会涉及到, 我们将看看如何将它用作一个I/O的输入和输出对象, 例如当做Fprintf的io.Writer输出对象, 或者当作io.Reader类型的输入源对象。< / p >
< p > < strong > 练习 3.10: < / strong > 编写一个非递归版本的comma函数, 使用bytes.Buffer代替字符串链接操作。< / p >
< p > < strong > 练习 3.11: < / strong > 完善comma函数, 以支持浮点数处理和一个可选的正负号的处理。< / p >
< p > < strong > 练习 3.12: < / strong > 编写一个函数,判断两个字符串是否是相互打乱的,也就是说它们有着相同的字符,但是对应不同的顺序。< / p >
2024-08-08 07:47:03 +00:00
< h3 > 3.5.5. 字符串和数字的转换< / h3 >
2022-08-04 06:59:33 +00:00
< p > 除了字符串、字符、字节之间的转换, 字符串和数值之间的转换也比较常见。由strconv包提供这类转换功能。< / p >
< p > 将一个整数转为字符串, 一种方法是用fmt.Sprintf返回一个格式化的字符串; 另一个方法是用strconv.Itoa(“整数到ASCII”): < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > x := 123
y := fmt.Sprintf(" %d" , x)
fmt.Println(y, strconv.Itoa(x)) // " 123 123"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > FormatInt和FormatUint函数可以用不同的进制来格式化数字: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > fmt.Println(strconv.FormatInt(int64(x), 2)) // " 1111011"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > fmt.Printf函数的%b、%d、%o和%x等参数提供功能往往比strconv包的Format函数方便很多, 特别是在需要包含有附加额外信息的时候: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > s := fmt.Sprintf(" x=%b" , x) // " x=1111011"
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > 如果要将一个字符串解析为整数, 可以使用strconv包的Atoi或ParseInt函数, 还有用于解析无符号整数的ParseUint函数: < / p >
2024-08-08 07:47:03 +00:00
< pre > < code class = "language-Go" > x, err := strconv.Atoi(" 123" ) // x is an int
y, err := strconv.ParseInt(" 123" , 10, 64) // base 10, up to 64 bits
2022-08-04 06:59:33 +00:00
< / code > < / pre >
< p > ParseInt函数的第三个参数是用于指定整型数的大小; 例如16表示int16, 0则表示int。在任何情况下, 返回的结果y总是int64类型, 你可以通过强制类型转换将它转为更小的整数类型。< / p >
< p > 有时候也会使用fmt.Scanf来解析输入的字符串和数字, 特别是当字符串和数字混合在一行的时候, 它可以灵活处理不完整或不规则的输入。< / p >
2024-08-08 07:47:03 +00:00
< hr > < table > < tr > < td > < img width = "222px" src = "https://chai2010.cn/advanced-go-programming-book/css.png" > < / td > < td > < img width = "222px" src = "https://chai2010.cn/advanced-go-programming-book/cch.png" > < / td > < / tr > < / table >
< div id = "giscus-container" > < / div >
2022-08-04 06:59:33 +00:00
2024-08-08 07:47:03 +00:00
< footer class = "page-footer" >
< span > © 2015-2016 | < a href = "https://github.com/gopl-zh" > Go语言圣经中文版< / a > , 仅学习交流使用< / span >
< / footer >
2022-08-04 06:59:33 +00:00
< / main >
< nav class = "nav-wrapper" aria-label = "Page navigation" >
<!-- Mobile navigation buttons -->
2024-08-08 07:47:03 +00:00
< a rel = "prev" href = "../ch3/ch3-04.html" class = "mobile-nav-chapters previous" title = "Previous chapter" aria-label = "Previous chapter" aria-keyshortcuts = "Left" >
2022-08-04 06:59:33 +00:00
< i class = "fa fa-angle-left" > < / i >
< / a >
2024-08-08 07:47:03 +00:00
<!-- ../ch3/ch3 - 06.html -->
< a rel = "next" href = "../ch3/ch3-06.html" class = "mobile-nav-chapters next" title = "Next chapter" aria-label = "Next chapter" aria-keyshortcuts = "Right" >
2022-08-04 06:59:33 +00:00
< i class = "fa fa-angle-right" > < / i >
< / a >
< div style = "clear: both" > < / div >
< / nav >
< / div >
< / div >
< nav class = "nav-wide-wrapper" aria-label = "Page navigation" >
2024-08-08 07:47:03 +00:00
< a rel = "prev" href = "../ch3/ch3-04.html" class = "nav-chapters previous" title = "Previous chapter" aria-label = "Previous chapter" aria-keyshortcuts = "Left" >
2022-08-04 06:59:33 +00:00
< i class = "fa fa-angle-left" > < / i >
< / a >
2024-08-08 07:47:03 +00:00
< a rel = "next" href = "../ch3/ch3-06.html" class = "nav-chapters next" title = "Next chapter" aria-label = "Next chapter" aria-keyshortcuts = "Right" >
2022-08-04 06:59:33 +00:00
< i class = "fa fa-angle-right" > < / i >
< / a >
2024-08-08 07:47:03 +00:00
< / nav >
2022-08-04 06:59:33 +00:00
< / div >
2024-08-08 07:47:03 +00:00
< script type = "text/javascript" >
2022-08-04 06:59:33 +00:00
window.playground_copyable = true;
< / script >
2024-08-08 07:47:03 +00:00
< script src = "../static/mnbook/mark.min.js" type = "text/javascript" charset = "utf-8" > < / script >
< script src = "../static/mnbook/clipboard.min.js" type = "text/javascript" charset = "utf-8" > < / script >
< script src = "../static/mnbook/highlight.js" type = "text/javascript" charset = "utf-8" > < / script >
< script src = "../static/mnbook/book.js" type = "text/javascript" charset = "utf-8" > < / script >
2022-08-04 06:59:33 +00:00
< script type = "text/javascript" charset = "utf-8" >
2024-08-08 07:47:03 +00:00
var pagePath = "ch3/ch3-05.md"
2022-08-04 06:59:33 +00:00
< / script >
<!-- Custom JS scripts -->
2024-08-08 07:47:03 +00:00
< script src = "../static/mnbook/giscus.js" type = "text/javascript" charset = "utf-8" > < / script >
2022-08-04 06:59:33 +00:00
2024-08-08 07:47:03 +00:00
2022-08-04 06:59:33 +00:00
< / body >
< / html >