gopl-zh.github.com/ch3/ch3-05.html

526 lines
54 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html lang="zh" class="sidebar-visible no-js light">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>字符串 - Go语言圣经</title>
<!-- Custom HTML head -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="&lt;The Go Programming Language&gt;中文版">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff" />
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="../style.css">
</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 {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script type="text/javascript">
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
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) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
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">
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Go语言圣经</a></li><li class="chapter-item expanded affix "><a href="../preface-zh.html">译者序</a></li><li class="chapter-item expanded affix "><a href="../preface.html">前言</a></li><li class="chapter-item expanded "><a href="../ch1/ch1.html"><strong aria-hidden="true">1.</strong> 入门</a></li><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><li class="chapter-item expanded "><a href="../ch2/ch2.html"><strong aria-hidden="true">2.</strong> 程序结构</a></li><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><li class="chapter-item expanded "><a href="../ch3/ch3.html"><strong aria-hidden="true">3.</strong> 基础数据类型</a></li><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><li class="chapter-item expanded "><a href="../ch4/ch4.html"><strong aria-hidden="true">4.</strong> 复合数据类型</a></li><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.<
<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="rust">Rust</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>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Go语言圣经</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/gopl-zh/gopl-zh.github.com" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<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">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</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>
<main>
<!-- 头部 -->
<ul dir="auto">
<li><em>Go语言圣经读书笔记(挖坑中...): <a href="https://github.com/chai2010/gopl-notes-zh">https://github.com/chai2010/gopl-notes-zh</a></em></li>
<li><em>凹语言(Go实现, 面向WASM设计): <a href="https://github.com/wa-lang/wa">https://github.com/wa-lang/wa</a></em></li>
</ul>
<hr>
<h2 id="35-字符串"><a class="header" href="#35-字符串">3.5. 字符串</a></h2>
<p>一个字符串是一个不可改变的字节序列。字符串可以包含任意的数据包括byte值0但是通常是用来包含人类可读的文本。文本字符串通常被解释为采用UTF8编码的Unicode码点rune序列我们稍后会详细讨论这个问题。</p>
<p>内置的len函数可以返回一个字符串中的字节数目不是rune字符数目索引操作s[i]返回第i个字节的字节值i必须满足0 ≤ i&lt; len(s)条件约束。</p>
<pre><code class="language-Go">s := &quot;hello, world&quot;
fmt.Println(len(s)) // &quot;12&quot;
fmt.Println(s[0], s[7]) // &quot;104 119&quot; ('h' and 'w')
</code></pre>
<p>如果试图访问超出字符串索引范围的字节将会导致panic异常</p>
<pre><code class="language-Go">c := s[len(s)] // panic: index out of range
</code></pre>
<p>第i个字节并不一定是字符串的第i个字符因为对于非ASCII字符的UTF8编码会要两个或多个字节。我们先简单说下字符的工作方式。</p>
<p>子字符串操作s[i:j]基于原始的s字符串的第i个字节开始到第j个字节并不包含j本身生成一个新字符串。生成的新字符串将包含j-i个字节。</p>
<pre><code class="language-Go">fmt.Println(s[0:5]) // &quot;hello&quot;
</code></pre>
<p>同样如果索引超出字符串范围或者j小于i的话将导致panic异常。</p>
<p>不管i还是j都可能被忽略当它们被忽略时将采用0作为开始位置采用len(s)作为结束的位置。</p>
<pre><code class="language-Go">fmt.Println(s[:5]) // &quot;hello&quot;
fmt.Println(s[7:]) // &quot;world&quot;
fmt.Println(s[:]) // &quot;hello, world&quot;
</code></pre>
<p>其中+操作符将两个字符串连接构造一个新字符串:</p>
<pre><code class="language-Go">fmt.Println(&quot;goodbye&quot; + s[5:]) // &quot;goodbye, world&quot;
</code></pre>
<p>字符串可以用==和&lt;进行比较;比较通过逐个字节比较完成的,因此比较的结果是字符串自然编码的顺序。</p>
<p>字符串的值是不可变的:一个字符串包含的字节序列永远不会被改变,当然我们也可以给一个字符串变量分配一个新字符串值。可以像下面这样将一个字符串追加到另一个字符串:</p>
<pre><code class="language-Go">s := &quot;left foot&quot;
t := s
s += &quot;, right foot&quot;
</code></pre>
<p>这并不会导致原始的字符串值被改变但是变量s将因为+=语句持有一个新的字符串值但是t依然是包含原先的字符串值。</p>
<pre><code class="language-Go">fmt.Println(s) // &quot;left foot, right foot&quot;
fmt.Println(t) // &quot;left foot&quot;
</code></pre>
<p>因为字符串是不可修改的,因此尝试修改字符串内部数据的操作也是被禁止的:</p>
<pre><code class="language-Go">s[0] = 'L' // compile error: cannot assign to s[0]
</code></pre>
<p>不变性意味着如果两个字符串共享相同的底层数据的话也是安全的这使得复制任何长度的字符串代价是低廉的。同样一个字符串s和对应的子字符串切片s[7:]的操作也可以安全地共享相同的内存,因此字符串切片操作代价也是低廉的。在这两种情况下都没有必要分配新的内存。 图3.4演示了一个字符串和两个子串共享相同的底层数据。</p>
<h3 id="351-字符串面值"><a class="header" href="#351-字符串面值">3.5.1. 字符串面值</a></h3>
<p>字符串值也可以用字符串面值方式编写,只要将一系列字节序列包含在双引号内即可:</p>
<pre><code>&quot;Hello, world&quot;
</code></pre>
<p><img src="../images/ch3-04.png" alt="" /></p>
<p>因为Go语言源文件总是用UTF8编码并且Go语言的文本字符串也以UTF8编码的方式处理因此我们可以将Unicode码点也写到字符串面值中。</p>
<p>在一个双引号包含的字符串面值中,可以用以反斜杠<code>\</code>开头的转义序列插入任意的数据。下面的换行、回车和制表符等是常见的ASCII控制代码的转义方式</p>
<pre><code>\a 响铃
\b 退格
\f 换页
\n 换行
\r 回车
\t 制表符
\v 垂直制表符
\' 单引号(只用在 '\'' 形式的rune符号面值中
\&quot; 双引号(只用在 &quot;...&quot; 形式的字符串面值中)
\\ 反斜杠
</code></pre>
<p>可以通过十六进制或八进制转义在字符串面值中包含任意的字节。一个十六进制的转义形式是<code>\xhh</code>其中两个h表示十六进制数字大写或小写都可以。一个八进制转义形式是<code>\ooo</code>包含三个八进制的o数字0到7但是不能超过<code>\377</code>译注对应一个字节的范围十进制为255。每一个单一的字节表达一个特定的值。稍后我们将看到如何将一个Unicode码点写到字符串面值中。</p>
<p>一个原生的字符串面值形式是`...`,使用反引号代替双引号。在原生的字符串面值中,没有转义操作;全部的内容都是字面的意思,包含退格和换行,因此一个程序中的原生字符串面值可能跨越多行(译注:在原生字符串面值内部是无法直接写`字符的,可以用八进制或十六进制转义或+&quot;`&quot;连接字符串常量完成。唯一的特殊处理是会删除回车以保证在所有平台上的值都是一样的包括那些把回车也放入文本文件的系统译注Windows系统会把回车和换行一起放入文本文件中</p>
<p>原生字符串面值用于编写正则表达式会很方便因为正则表达式往往会包含很多反斜杠。原生字符串面值同时被广泛应用于HTML模板、JSON面值、命令行提示信息以及那些需要扩展到多行的场景。</p>
<pre><code class="language-Go">const GoUsage = `Go is a tool for managing Go source code.
Usage:
go command [arguments]
...`
</code></pre>
<h3 id="352-unicode"><a class="header" href="#352-unicode">3.5.2. Unicode</a></h3>
<p>在很久以前世界还是比较简单的起码计算机世界就只有一个ASCII字符集美国信息交换标准代码。ASCII更准确地说是美国的ASCII使用7bit来表示128个字符包含英文字母的大小写、数字、各种标点符号和设备控制符。对于早期的计算机程序来说这些就足够了但是这也导致了世界上很多其他地区的用户无法直接使用自己的符号系统。随着互联网的发展混合多种语言的数据变得很常见译注比如本身的英文原文或中文翻译都包含了ASCII、中文、日文等多种语言字符。如何有效处理这些包含了各种语言的丰富多样的文本数据呢</p>
<p>答案就是使用Unicode http://unicode.org 它收集了这个世界上所有的符号系统包括重音符号和其它变音符号制表符和回车符还有很多神秘的符号每个符号都分配一个唯一的Unicode码点Unicode码点对应Go语言中的rune整数类型译注rune是int32等价类型</p>
<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>
<h3 id="353-utf-8"><a class="header" href="#353-utf-8">3.5.3. UTF-8</a></h3>
<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>
<pre><code>0xxxxxxx runes 0-127 (ASCII)
110xxxxx 10xxxxxx 128-2047 (values &lt;128 unused)
1110xxxx 10xxxxxx 10xxxxxx 2048-65535 (values &lt;2048 unused)
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 65536-0x10ffff (other values unused)
</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>
<pre><code>&quot;世界&quot;
&quot;\xe4\xb8\x96\xe7\x95\x8c&quot;
&quot;\u4e16\u754c&quot;
&quot;\U00004e16\U0000754c&quot;
</code></pre>
<p>上面三个转义序列都为第一个字符串提供替代写法,但是它们的值都是相同的。</p>
<p>Unicode转义也可以使用在rune字符中。下面三个字符是等价的</p>
<pre><code>'世' '\u4e16' '\U00004e16'
</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>
<pre><code class="language-Go">func HasPrefix(s, prefix string) bool {
return len(s) &gt;= len(prefix) &amp;&amp; s[:len(prefix)] == prefix
}
</code></pre>
<p>或者是后缀测试:</p>
<pre><code class="language-Go">func HasSuffix(s, suffix string) bool {
return len(s) &gt;= len(suffix) &amp;&amp; s[len(s)-len(suffix):] == suffix
}
</code></pre>
<p>或者是包含子串测试:</p>
<pre><code class="language-Go">func Contains(s, substr string) bool {
for i := 0; i &lt; len(s); i++ {
if HasPrefix(s[i:], substr) {
return true
}
}
return false
}
</code></pre>
<p>对于UTF8编码后文本的处理和原始的字节处理逻辑是一样的。但是对应很多其它编码则并不是这样的。上面的函数都来自strings字符串处理包真实的代码包含了一个用哈希技术优化的Contains 实现。)</p>
<p>另一方面如果我们真的关心每个Unicode字符我们可以使用其它处理方式。考虑前面的第一个例子中的字符串它混合了中西两种字符。图3.5展示了它的内存表示形式。字符串包含13个字节以UTF8形式编码但是只对应9个Unicode字符</p>
<pre><code class="language-Go">import &quot;unicode/utf8&quot;
s := &quot;Hello, 世界&quot;
fmt.Println(len(s)) // &quot;13&quot;
fmt.Println(utf8.RuneCountInString(s)) // &quot;9&quot;
</code></pre>
<p>为了处理这些真实的字符我们需要一个UTF8解码器。unicode/utf8包提供了该功能我们可以这样使用</p>
<pre><code class="language-Go">for i := 0; i &lt; len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
fmt.Printf(&quot;%d\t%c\n&quot;, i, r)
i += size
}
</code></pre>
<p>每一次调用DecodeRuneInString函数都返回一个r和长度r对应字符本身长度对应r采用UTF8编码后的编码字节数目。长度可以用于更新第i个字符在字符串中的字节索引位置。但是这种编码方式是笨拙的我们需要更简洁的语法。幸运的是Go语言的range循环在处理字符串的时候会自动隐式解码UTF8字符串。下面的循环运行如图3.5所示需要注意的是对于非ASCII索引更新的步长将超过1个字节。</p>
<p><img src="../images/ch3-05.png" alt="" /></p>
<pre><code class="language-Go">for i, r := range &quot;Hello, 世界&quot; {
fmt.Printf(&quot;%d\t%q\t%d\n&quot;, i, r, r)
}
</code></pre>
<p>我们可以使用一个简单的循环来统计字符串中字符的数目,像这样:</p>
<pre><code class="language-Go">n := 0
for _, _ = range s {
n++
}
</code></pre>
<p>像其它形式的循环那样,我们也可以忽略不需要的变量:</p>
<pre><code class="language-Go">n := 0
for range s {
n++
}
</code></pre>
<p>或者我们可以直接调用utf8.RuneCountInString(s)函数。</p>
<p>正如我们前面提到的文本字符串采用UTF8编码只是一种惯例但是对于循环的真正字符串并不是一个惯例这是正确的。如果用于循环的字符串只是一个普通的二进制数据或者是含有错误编码的UTF8数据将会发生什么呢</p>
<p>每一个UTF8字符解码不管是显式地调用utf8.DecodeRuneInString解码或是在range循环中隐式地解码如果遇到一个错误的UTF8编码输入将生成一个特别的Unicode字符<code>\uFFFD</code>,在印刷中这个符号通常是一个黑色六角或钻石形状,里面包含一个白色的问号&quot;?&quot;。当程序遇到这样的一个字符通常是一个危险信号说明输入并不是一个完美没有错误的UTF8字符串。</p>
<p>UTF8字符串作为交换格式是非常方便的但是在程序内部采用rune序列可能更方便因为rune大小一致支持数组索引和方便切割。</p>
<p>将[]rune类型转换应用到UTF8编码的字符串将返回字符串编码的Unicode码点序列</p>
<pre><code class="language-Go">// &quot;program&quot; in Japanese katakana
s := &quot;プログラム&quot;
fmt.Printf(&quot;% x\n&quot;, s) // &quot;e3 83 97 e3 83 ad e3 82 b0 e3 83 a9 e3 83 a0&quot;
r := []rune(s)
fmt.Printf(&quot;%x\n&quot;, r) // &quot;[30d7 30ed 30b0 30e9 30e0]&quot;
</code></pre>
<p>在第一个Printf中的<code>% x</code>参数用于在每个十六进制数字前插入一个空格。)</p>
<p>如果是将一个[]rune类型的Unicode字符slice或数组转为string则对它们进行UTF8编码</p>
<pre><code class="language-Go">fmt.Println(string(r)) // &quot;プログラム&quot;
</code></pre>
<p>将一个整数转型为字符串意思是生成以只包含对应Unicode码点字符的UTF8字符串</p>
<pre><code class="language-Go">fmt.Println(string(65)) // &quot;A&quot;, not &quot;65&quot;
fmt.Println(string(0x4eac)) // &quot;&quot;
</code></pre>
<p>如果对应码点的字符是无效的,则用<code>\uFFFD</code>无效字符作为替换:</p>
<pre><code class="language-Go">fmt.Println(string(1234567)) // &quot;?&quot;
</code></pre>
<h3 id="354-字符串和byte切片"><a class="header" href="#354-字符串和byte切片">3.5.4. 字符串和Byte切片</a></h3>
<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>
<pre><code class="language-Go">fmt.Println(basename(&quot;a/b/c.go&quot;)) // &quot;c&quot;
fmt.Println(basename(&quot;c.d.go&quot;)) // &quot;c.d&quot;
fmt.Println(basename(&quot;abc&quot;)) // &quot;abc&quot;
</code></pre>
<p>第一个版本并没有使用任何库,全部手工硬编码实现:</p>
<p><u><i>gopl.io/ch3/basename1</i></u></p>
<pre><code class="language-Go">// basename removes directory components and a .suffix.
// e.g., a =&gt; a, a.go =&gt; a, a/b/c.go =&gt; c, a/b.c.go =&gt; b.c
func basename(s string) string {
// Discard last '/' and everything before.
for i := len(s) - 1; i &gt;= 0; i-- {
if s[i] == '/' {
s = s[i+1:]
break
}
}
// Preserve everything before last '.'.
for i := len(s) - 1; i &gt;= 0; i-- {
if s[i] == '.' {
s = s[:i]
break
}
}
return s
}
</code></pre>
<p>这个简化版本使用了strings.LastIndex库函数</p>
<p><u><i>gopl.io/ch3/basename2</i></u></p>
<pre><code class="language-Go">func basename(s string) string {
slash := strings.LastIndex(s, &quot;/&quot;) // -1 if &quot;/&quot; not found
s = s[slash+1:]
if dot := strings.LastIndex(s, &quot;.&quot;); dot &gt;= 0 {
s = s[:dot]
}
return s
}
</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>
<p><u><i>gopl.io/ch3/comma</i></u></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 &lt;= 3 {
return s
}
return comma(s[:n-3]) + &quot;,&quot; + s[n-3:]
}
</code></pre>
<p>输入comma函数的参数是一个字符串。如果输入字符串的长度小于或等于3的话则不需要插入逗号分隔符。否则comma函数将在最后三个字符前的位置将字符串切割为两个子串并插入逗号分隔符然后通过递归调用自身来得出前面的子串。</p>
<p>一个字符串是包含只读字节的数组一旦创建是不可变的。相比之下一个字节slice的元素则可以自由地修改。</p>
<p>字符串和字节slice之间可以相互转换</p>
<pre><code class="language-Go">s := &quot;abc&quot;
b := []byte(s)
s2 := string(b)
</code></pre>
<p>从概念上讲,一个[]byte(s)转换是分配了一个新的字节数组用于保存字符串数据的拷贝然后引用这个底层的字节数组。编译器的优化可以避免在一些场景下分配和复制字符串数据但总的来说需要确保在变量b被修改的情况下原始的s字符串也不会改变。将一个字节slice转换到字符串的string(b)操作则是构造一个字符串拷贝以确保s2字符串是只读的。</p>
<p>为了避免转换中不必要的内存分配bytes包和strings同时提供了许多实用函数。下面是strings包中的六个函数</p>
<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
</code></pre>
<p>bytes包中也对应的六个函数</p>
<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
</code></pre>
<p>它们之间唯一的区别是字符串类型参数被替换成了字节slice类型的参数。</p>
<p>bytes包还提供了Buffer类型用于字节slice的缓存。一个Buffer开始是空的但是随着string、byte或[]byte等类型数据的写入可以动态增长一个bytes.Buffer变量并不需要初始化因为零值也是有效的</p>
<p><u><i>gopl.io/ch3/printints</i></u></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 &gt; 0 {
buf.WriteString(&quot;, &quot;)
}
fmt.Fprintf(&amp;buf, &quot;%d&quot;, v)
}
buf.WriteByte(']')
return buf.String()
}
func main() {
fmt.Println(intsToString([]int{1, 2, 3})) // &quot;[1, 2, 3]&quot;
}
</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>
<h3 id="355-字符串和数字的转换"><a class="header" href="#355-字符串和数字的转换">3.5.5. 字符串和数字的转换</a></h3>
<p>除了字符串、字符、字节之间的转换字符串和数值之间的转换也比较常见。由strconv包提供这类转换功能。</p>
<p>将一个整数转为字符串一种方法是用fmt.Sprintf返回一个格式化的字符串另一个方法是用strconv.Itoa(“整数到ASCII”)</p>
<pre><code class="language-Go">x := 123
y := fmt.Sprintf(&quot;%d&quot;, x)
fmt.Println(y, strconv.Itoa(x)) // &quot;123 123&quot;
</code></pre>
<p>FormatInt和FormatUint函数可以用不同的进制来格式化数字</p>
<pre><code class="language-Go">fmt.Println(strconv.FormatInt(int64(x), 2)) // &quot;1111011&quot;
</code></pre>
<p>fmt.Printf函数的%b、%d、%o和%x等参数提供功能往往比strconv包的Format函数方便很多特别是在需要包含有附加额外信息的时候</p>
<pre><code class="language-Go">s := fmt.Sprintf(&quot;x=%b&quot;, x) // &quot;x=1111011&quot;
</code></pre>
<p>如果要将一个字符串解析为整数可以使用strconv包的Atoi或ParseInt函数还有用于解析无符号整数的ParseUint函数</p>
<pre><code class="language-Go">x, err := strconv.Atoi(&quot;123&quot;) // x is an int
y, err := strconv.ParseInt(&quot;123&quot;, 10, 64) // base 10, up to 64 bits
</code></pre>
<p>ParseInt函数的第三个参数是用于指定整型数的大小例如16表示int160则表示int。在任何情况下返回的结果y总是int64类型你可以通过强制类型转换将它转为更小的整数类型。</p>
<p>有时候也会使用fmt.Scanf来解析输入的字符串和数字特别是当字符串和数字混合在一行的时候它可以灵活处理不完整或不规则的输入。</p>
<!-- 公众号 -->
<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>
<footer class="page-footer">
<span>© 2015-2016 | <a href="https://github.com/gopl-zh"> Go语言圣经中文版</a>, 仅学习交流使用</span>
</footer>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../ch3/ch3-04.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next" href="../ch3/ch3-06.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<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">
<a rel="prev" href="../ch3/ch3-04.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next" href="../ch3/ch3-06.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script type="text/javascript">
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var pagePath = "ch3/ch3-05.md"
</script>
<!-- Custom JS scripts -->
<script type="text/javascript" src="../js/custom.js"></script>
<script type="text/javascript" src="../js/bigPicture.js"></script>
</body>
</html>