From d06b4c36c591e772481543fdec4c7fe0453115f0 Mon Sep 17 00:00:00 2001 From: jiasir Date: Mon, 21 Mar 2022 08:21:36 +0800 Subject: [PATCH] feat: add strings and runes (#81) --- PROGRESS.md | 3 +- README.md | 2 +- examples.txt | 1 + .../strings-and-runes/strings-and-runes.go | 67 +++++++++++++++++++ .../strings-and-runes/strings-and-runes.hash | 2 + .../strings-and-runes/strings-and-runes.sh | 20 ++++++ 6 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 examples/strings-and-runes/strings-and-runes.go create mode 100644 examples/strings-and-runes/strings-and-runes.hash create mode 100644 examples/strings-and-runes/strings-and-runes.sh diff --git a/PROGRESS.md b/PROGRESS.md index 7877f00e..f834b755 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -1,6 +1,6 @@ # 期待您的参与 -例子已完成与[源项目](https://github.com/mmcgrana/gobyexample) master 分支(截止 2022 年 3 月 19 日)的同步,均为 78 个例子。 +例子已完成与[源项目](https://github.com/mmcgrana/gobyexample) master 分支(截止 2022 年 3 月 20 日)的同步,均为 79 个例子。 后续如果你发现有更新不及时的情况,或者你觉得目前的翻译内容有任何问题,我们都 `非常欢迎` 各位同学提交 pull request。 @@ -23,6 +23,7 @@ - [x] Closures->闭包 - [x] Recursion->递归 - [x] Pointers->指针 +- [x] Strings and Runes->字符串和rune类型 - [x] Structs->结构体 - [x] Methods->方法 - [x] Interfaces->接口 diff --git a/README.md b/README.md index 58fdc54a..b644c3ea 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Go Gopher 的版权归 [Renée French](http://reneefrench.blogspot.com/) 所有 1. `tools/serve` 本地预览效果; 1. 通过自测后即可提交 pull request :) -项目现由 [gobyexample-cn](https://github.com/gobyexample-cn) 维护,例子已完全与英文版同步(截止 2022-3-19),均为 78 个,可以在这里查看 [同步进度](PROGRESS.md)。 +项目现由 [gobyexample-cn](https://github.com/gobyexample-cn) 维护,例子已完全与英文版同步(截止 2022-3-20),均为 79 个,可以在这里查看 [同步进度](PROGRESS.md)。 后续可能会出现与英文版同步不及时的情况,`非常欢迎` 各位同学 fork 并提交 pull request。 diff --git a/examples.txt b/examples.txt index a2956d2f..f0b9fdf0 100644 --- a/examples.txt +++ b/examples.txt @@ -15,6 +15,7 @@ Variadic Functions->变参函数 Closures->闭包 Recursion->递归 Pointers->指针 +Strings and Runes->字符串和rune类型 Structs->结构体 Methods->方法 Interfaces->接口 diff --git a/examples/strings-and-runes/strings-and-runes.go b/examples/strings-and-runes/strings-and-runes.go new file mode 100644 index 00000000..9d3a3a33 --- /dev/null +++ b/examples/strings-and-runes/strings-and-runes.go @@ -0,0 +1,67 @@ +// Go语言中的字符串是一个只读的byte类型的切片。 +// Go语言和标准库特别对待字符串 - 作为以 +// [UTF-8](https://en.wikipedia.org/wiki/UTF-8) 为编码的文本容器。 +// 在其他语言当中, 字符串由"字符"组成。 +// 在Go语言当中,字符的概念被称为 `rune` - 它是一个表示 +// Unicode 编码的整数。 +// [这个Go博客](https://go.dev/blog/strings) 很好的介绍了这个主题。 + +package main + +import ( + "fmt" + "unicode/utf8" +) + +func main() { + + // `s` 是一个 `string` 分配了一个 literal value + // 表示泰语中的单词 "hello" 。 + // Go 字符串是 UTF-8 编码的文本。 + const s = "สวัสดี" + + // 因为字符串等价于 `[]byte`, + // 这会产生存储在其中的原始字节的长度。 + fmt.Println("Len:", len(s)) + + // 对字符串进行索引会在每个索引处生成原始字节值。 + // 这个循环生成构成`s`中 Unicode 的所有字节的十六进制值。 + for i := 0; i < len(s); i++ { + fmt.Printf("%x ", s[i]) + } + fmt.Println() + + // 要计算字符串中有多少rune,我们可以使用`utf8`包。 + // 注意`RuneCountInString`的运行时取决于字符串的大小。 + // 因为它必须按顺序解码每个 UTF-8 rune。 + // 一些泰语字符由多个 UTF-8 code point 表示, + // 所以这个计数的结果可能会令人惊讶。 + fmt.Println("Rune count:", utf8.RuneCountInString(s)) + + // `range` 循环专门处理字符串并解码每个 `rune` 及其在字符串中的偏移量。 + for idx, runeValue := range s { + fmt.Printf("%#U starts at %d\n", runeValue, idx) + } + + // 我们可以通过显式使用 `utf8.DecodeRuneInString` 函数来实现相同的迭代。 + fmt.Println("\nUsing DecodeRuneInString") + for i, w := 0, 0; i < len(s); i += w { + runeValue, width := utf8.DecodeRuneInString(s[i:]) + fmt.Printf("%#U starts at %d\n", runeValue, i) + w = width + + // 这演示了将 `rune` value 传递给函数。 + examineRune(runeValue) + } +} + +func examineRune(r rune) { + + // 用单引号括起来的值是 _rune literals_. + // 我们可以直接将 `rune` value 与 rune literal 进行比较。 + if r == 't' { + fmt.Println("found tee") + } else if r == 'ส' { + fmt.Println("found so sua") + } +} diff --git a/examples/strings-and-runes/strings-and-runes.hash b/examples/strings-and-runes/strings-and-runes.hash new file mode 100644 index 00000000..e3397130 --- /dev/null +++ b/examples/strings-and-runes/strings-and-runes.hash @@ -0,0 +1,2 @@ +516ce5b3ab2fd9b79ed941712073815187b2cfe7 +cgf_KS1aY4m diff --git a/examples/strings-and-runes/strings-and-runes.sh b/examples/strings-and-runes/strings-and-runes.sh new file mode 100644 index 00000000..f423a50e --- /dev/null +++ b/examples/strings-and-runes/strings-and-runes.sh @@ -0,0 +1,20 @@ +$ go run strings-and-runes.go +Len: 18 +e0 b8 aa e0 b8 a7 e0 b8 b1 e0 b8 aa e0 b8 94 e0 b8 b5 +Rune count: 6 +U+0E2A 'ส' starts at 0 +U+0E27 'ว' starts at 3 +U+0E31 'ั' starts at 6 +U+0E2A 'ส' starts at 9 +U+0E14 'ด' starts at 12 +U+0E35 'ี' starts at 15 + +Using DecodeRuneInString +U+0E2A 'ส' starts at 0 +found so sua +U+0E27 'ว' starts at 3 +U+0E31 'ั' starts at 6 +U+0E2A 'ส' starts at 9 +found so sua +U+0E14 'ด' starts at 12 +U+0E35 'ี' starts at 15