From cca7879dce86a1050e3e408136b24cd2585d9aaf Mon Sep 17 00:00:00 2001
From: Mark McGranaghan
Date: Tue, 27 Dec 2016 07:41:08 -0800
Subject: [PATCH] Add note about len and [] operating on bytes
Fixes #148.
---
examples/string-functions/string-functions.go | 26 ++++++----
.../string-functions/string-functions.hash | 4 +-
public/string-functions | 48 +++++++++++--------
3 files changed, 45 insertions(+), 33 deletions(-)
diff --git a/examples/string-functions/string-functions.go b/examples/string-functions/string-functions.go
index 6ac0d3d..eea809a 100644
--- a/examples/string-functions/string-functions.go
+++ b/examples/string-functions/string-functions.go
@@ -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.
diff --git a/examples/string-functions/string-functions.hash b/examples/string-functions/string-functions.hash
index c4a0d65..93ae1ea 100644
--- a/examples/string-functions/string-functions.hash
+++ b/examples/string-functions/string-functions.hash
@@ -1,2 +1,2 @@
-d7150ce50772abdaf827082093613134cb05e08f
-Gkc5rDaeaN
+17aa523bbd606fa0b624fae44b89812d46330755
+Lf5_Zbg6or
diff --git a/public/string-functions b/public/string-functions
index 8438e99..30b2914 100644
--- a/public/string-functions
+++ b/public/string-functions
@@ -41,7 +41,7 @@ to give you a sense of the package.
-
+
@@ -90,10 +90,12 @@ it a lot below.
|
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
+package docs.
|
@@ -118,24 +120,12 @@ as the first argument to the function.
|
- You can find more functions in the strings
-package docs.
+ 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.
|
-
-
-
- |
-
-
-
-
- Not part of strings but worth mentioning here are
-the mechanisms for getting the length of a string
-and getting a character by index.
-
- |
-
+ |
p("Len: ", len("hello"))
p("Char:", "hello"[1])
@@ -145,6 +135,22 @@ and getting a character by index.
|
+
+
+ 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
+for more information.
+
+ |
+
+
+
+ |
+
+