forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example5.go
36 lines (27 loc) · 935 Bytes
/
example5.go
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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// https://play.golang.org/p/W3c_iWsvqj
// Sample program to show how strings have a UTF-8 encoded byte array.
package main
import (
"fmt"
"unicode/utf8"
)
// main is the entry point for the application.
func main() {
// Declare a string with both chinese and english characters.
s := "世界 means world"
// UTFMax is 4 -- up to 4 bytes per encoded rune.
var buf [utf8.UTFMax]byte
// Iterate over each character in the string.
for i, r := range s {
// Capture the number of bytes for this character.
rl := utf8.RuneLen(r)
// Calculate the slice offset to slice the character out.
si := i + rl
// Copy of character from the string to our buffer.
copy(buf[:], s[i:si])
// Display the details.
fmt.Printf("%2d: %q; codepoint: %#6x; encoded bytes: %#v\n", i, r, r, buf[:rl])
}
}