Skip to content

Commit d06b4c3

Browse files
authored
feat: add strings and runes (#81)
1 parent 1b78f0f commit d06b4c3

File tree

6 files changed

+93
-2
lines changed

6 files changed

+93
-2
lines changed

PROGRESS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 期待您的参与
22

3-
例子已完成与[源项目](https://github.com/mmcgrana/gobyexample) master 分支(截止 2022 年 3 月 19 日)的同步,均为 78 个例子。
3+
例子已完成与[源项目](https://github.com/mmcgrana/gobyexample) master 分支(截止 2022 年 3 月 20 日)的同步,均为 79 个例子。
44

55
后续如果你发现有更新不及时的情况,或者你觉得目前的翻译内容有任何问题,我们都 `非常欢迎` 各位同学提交 pull request。
66

@@ -23,6 +23,7 @@
2323
- [x] Closures->闭包
2424
- [x] Recursion->递归
2525
- [x] Pointers->指针
26+
- [x] Strings and Runes->字符串和rune类型
2627
- [x] Structs->结构体
2728
- [x] Methods->方法
2829
- [x] Interfaces->接口

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Go Gopher 的版权归 [Renée French](http://reneefrench.blogspot.com/) 所有
8888
1. `tools/serve` 本地预览效果;
8989
1. 通过自测后即可提交 pull request :)
9090

91-
项目现由 [gobyexample-cn](https://github.com/gobyexample-cn) 维护,例子已完全与英文版同步(截止 2022-3-19),均为 78 个,可以在这里查看 [同步进度](PROGRESS.md)
91+
项目现由 [gobyexample-cn](https://github.com/gobyexample-cn) 维护,例子已完全与英文版同步(截止 2022-3-20),均为 79 个,可以在这里查看 [同步进度](PROGRESS.md)
9292

9393
后续可能会出现与英文版同步不及时的情况,`非常欢迎` 各位同学 fork 并提交 pull request。
9494

examples.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Variadic Functions->变参函数
1515
Closures->闭包
1616
Recursion->递归
1717
Pointers->指针
18+
Strings and Runes->字符串和rune类型
1819
Structs->结构体
1920
Methods->方法
2021
Interfaces->接口
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Go语言中的字符串是一个只读的byte类型的切片。
2+
// Go语言和标准库特别对待字符串 - 作为以
3+
// [UTF-8](https://en.wikipedia.org/wiki/UTF-8) 为编码的文本容器。
4+
// 在其他语言当中, 字符串由"字符"组成。
5+
// 在Go语言当中,字符的概念被称为 `rune` - 它是一个表示
6+
// Unicode 编码的整数。
7+
// [这个Go博客](https://go.dev/blog/strings) 很好的介绍了这个主题。
8+
9+
package main
10+
11+
import (
12+
"fmt"
13+
"unicode/utf8"
14+
)
15+
16+
func main() {
17+
18+
// `s` 是一个 `string` 分配了一个 literal value
19+
// 表示泰语中的单词 "hello" 。
20+
// Go 字符串是 UTF-8 编码的文本。
21+
const s = "สวัสดี"
22+
23+
// 因为字符串等价于 `[]byte`,
24+
// 这会产生存储在其中的原始字节的长度。
25+
fmt.Println("Len:", len(s))
26+
27+
// 对字符串进行索引会在每个索引处生成原始字节值。
28+
// 这个循环生成构成`s`中 Unicode 的所有字节的十六进制值。
29+
for i := 0; i < len(s); i++ {
30+
fmt.Printf("%x ", s[i])
31+
}
32+
fmt.Println()
33+
34+
// 要计算字符串中有多少rune,我们可以使用`utf8`包。
35+
// 注意`RuneCountInString`的运行时取决于字符串的大小。
36+
// 因为它必须按顺序解码每个 UTF-8 rune。
37+
// 一些泰语字符由多个 UTF-8 code point 表示,
38+
// 所以这个计数的结果可能会令人惊讶。
39+
fmt.Println("Rune count:", utf8.RuneCountInString(s))
40+
41+
// `range` 循环专门处理字符串并解码每个 `rune` 及其在字符串中的偏移量。
42+
for idx, runeValue := range s {
43+
fmt.Printf("%#U starts at %d\n", runeValue, idx)
44+
}
45+
46+
// 我们可以通过显式使用 `utf8.DecodeRuneInString` 函数来实现相同的迭代。
47+
fmt.Println("\nUsing DecodeRuneInString")
48+
for i, w := 0, 0; i < len(s); i += w {
49+
runeValue, width := utf8.DecodeRuneInString(s[i:])
50+
fmt.Printf("%#U starts at %d\n", runeValue, i)
51+
w = width
52+
53+
// 这演示了将 `rune` value 传递给函数。
54+
examineRune(runeValue)
55+
}
56+
}
57+
58+
func examineRune(r rune) {
59+
60+
// 用单引号括起来的值是 _rune literals_.
61+
// 我们可以直接将 `rune` value 与 rune literal 进行比较。
62+
if r == 't' {
63+
fmt.Println("found tee")
64+
} else if r == 'ส' {
65+
fmt.Println("found so sua")
66+
}
67+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
516ce5b3ab2fd9b79ed941712073815187b2cfe7
2+
cgf_KS1aY4m
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
$ go run strings-and-runes.go
2+
Len: 18
3+
e0 b8 aa e0 b8 a7 e0 b8 b1 e0 b8 aa e0 b8 94 e0 b8 b5
4+
Rune count: 6
5+
U+0E2A '' starts at 0
6+
U+0E27 '' starts at 3
7+
U+0E31 '' starts at 6
8+
U+0E2A '' starts at 9
9+
U+0E14 '' starts at 12
10+
U+0E35 '' starts at 15
11+
12+
Using DecodeRuneInString
13+
U+0E2A '' starts at 0
14+
found so sua
15+
U+0E27 '' starts at 3
16+
U+0E31 '' starts at 6
17+
U+0E2A '' starts at 9
18+
found so sua
19+
U+0E14 '' starts at 12
20+
U+0E35 '' starts at 15

0 commit comments

Comments
 (0)