Add note about len and [] operating on bytes

Fixes #148.
This commit is contained in:
Mark McGranaghan
2016-12-27 07:41:08 -08:00
parent e533ad365b
commit cca7879dce
3 changed files with 45 additions and 33 deletions

View File

@@ -41,7 +41,7 @@ to give you a sense of the package.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/Gkc5rDaeaN"><img title="Run code" src="play.png" class="run" /></a>
<a href="http://play.golang.org/p/Lf5_Zbg6or"><img title="Run code" src="play.png" class="run" /></a>
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
@@ -90,10 +90,12 @@ it a lot below.</p>
<tr>
<td class="docs">
<p>Here&rsquo;s a sample of the functions available in
<code>strings</code>. Note that these are all functions from the
package, not methods on the string object itself.
This means that we need pass the string in question
as the first argument to the function.</p>
<code>strings</code>. Since these are functions from the
package, not methods on the string object itself,
we need pass the string in question as the first
argument to the function. You can find more
functions in the <a href="http://golang.org/pkg/strings/"><code>strings</code></a>
package docs.</p>
</td>
<td class="code leading">
@@ -118,24 +120,12 @@ as the first argument to the function.</p>
<tr>
<td class="docs">
<p>You can find more functions in the <a href="http://golang.org/pkg/strings/"><code>strings</code></a>
package docs.</p>
<p>Not part of <code>strings</code>, but worth mentioning here, are
the mechanisms for getting the length of a string in
bytes and getting a byte by index.</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
<p>Not part of <code>strings</code> but worth mentioning here are
the mechanisms for getting the length of a string
and getting a character by index.</p>
</td>
<td class="code">
<td class="code leading">
<div class="highlight"><pre> <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Len: &quot;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="s">&quot;hello&quot;</span><span class="p">))</span>
<span class="nx">p</span><span class="p">(</span><span class="s">&quot;Char:&quot;</span><span class="p">,</span> <span class="s">&quot;hello&quot;</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
@@ -145,6 +135,22 @@ and getting a character by index.</p>
</td>
</tr>
<tr>
<td class="docs">
<p>Note that <code>len</code> and indexing above work at the byte level.
Go uses UTF-8 encoded strings, so this is often useful
as-is. If you&rsquo;re working with potentially multi-byte
characters you&rsquo;ll want to use encoding-aware operations.
See <a href="https://blog.golang.org/strings">strings, bytes, runes and characters in Go</a>
for more information.</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<table>