-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (50 loc) · 1.74 KB
/
main.go
File metadata and controls
57 lines (50 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// A So string is a read-only slice of bytes. The language
// and standard library handle strings in a special way -
// as containers for text encoded in UTF-8.
//
// In other languages, strings are made of "characters".
// In So, the concept of a character is called a `rune` -
// it's an integer that represents a Unicode code point.
package main
import (
"solod.dev/so/fmt"
"solod.dev/so/unicode/utf8"
)
func main() {
// `s` is a `string` assigned a literal value representing the
// word "hello" in the Thai language. So string literals are
// UTF-8 encoded text.
const s = "สวัสดี"
// Since a string is the same as `[]byte`, `len(s)`
// gives you the number of raw bytes stored inside.
println("len =", len(s))
// Indexing into a string gives you the raw byte values
// at each position. This loop prints the hex values of
// all the bytes that make up the code points in `s`.
for i := range len(s) {
fmt.Printf("%x ", s[i])
}
println()
// To count how many runes are in a string, we can use the
// `so/utf8` package. This is an O(n) time operation, because
// it has to decode each UTF-8 rune sequentially.
// Some Thai characters are represented by multi-byte UTF-8 code
// points, so the result of this count may be surprising.
n := utf8.RuneCountInString(s)
println("rune count =", n)
// A `range` loop handles strings specially and yields
// each rune along with its offset in the string.
for idx, r := range s {
fmt.Printf("0x%x starts at %d\n", r, int32(idx))
examineRune(r)
}
}
func examineRune(r rune) {
// Values enclosed in single quotes are rune literals.
// You can compare a rune value to a rune literal directly.
if r == 't' {
println("found tee")
} else if r == 'ส' {
println("found so sua")
}
}