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