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

@ -14,10 +14,12 @@ var p = fmt.Println
func main() {
// Here's a sample of the functions available in
// `strings`. 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.
// `strings`. 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 [`strings`](http://golang.org/pkg/strings/)
// package docs.
p("Contains: ", s.Contains("test", "es"))
p("Count: ", s.Count("test", "t"))
p("HasPrefix: ", s.HasPrefix("test", "te"))
@ -32,12 +34,16 @@ func main() {
p("ToUpper: ", s.ToUpper("test"))
p()
// You can find more functions in the [`strings`](http://golang.org/pkg/strings/)
// package docs.
// Not part of `strings` but worth mentioning here are
// the mechanisms for getting the length of a string
// and getting a character by index.
// Not part of `strings`, but worth mentioning here, are
// the mechanisms for getting the length of a string in
// bytes and getting a byte by index.
p("Len: ", len("hello"))
p("Char:", "hello"[1])
}
// Note that `len` and indexing above work at the byte level.
// Go uses UTF-8 encoded strings, so this is often useful
// as-is. If you're working with potentially multi-byte
// characters you'll want to use encoding-aware operations.
// See [strings, bytes, runes and characters in Go](https://blog.golang.org/strings)
// for more information.

View File

@ -1,2 +1,2 @@
d7150ce50772abdaf827082093613134cb05e08f
Gkc5rDaeaN
17aa523bbd606fa0b624fae44b89812d46330755
Lf5_Zbg6or

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>