-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 1.16 KB
/
main.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package stringsrunes
import (
"fmt"
"unicode/utf8"
)
/*
In Go, strings are a read only slice of bytes. The language and the standard
library treat strings specially, as containers (sequences) of text encoded
in UTF-8. In other languages, strings are made of up characters or `char` types.
In Go, the concept of a character is known as a rune, which is an integer that
represents a Unicode code point.
*/
func Run() {
basicString()
nonAsciiString()
compareRunes()
}
func basicString() {
// assign a string and print it.
myString := "foo"
fmt.Println(myString)
}
func nonAsciiString() {
// Thai word for 'hello'
const hello = "สวัสดี"
// Strings have a length, since they are equivalent to a byte slice, this produces
// the length of the raw bytes stored within.
fmt.Printf("length of %s is %d\n", hello, len(hello))
for i := 0; i < len(hello); i++ {
fmt.Printf("rune %d\n", hello[i])
}
// Print out the total number of runes
fmt.Printf("%s has a total of %d runes\n", hello, utf8.RuneCountInString(hello))
}
func compareRunes() {
myRune := 'A'
if myRune == 'A' {
fmt.Println("Found A!")
} else if myRune == 'B' {
fmt.Println("Found B!")
}
}