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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main

import (
"fmt"
// "strings"
)

// import "math"

func main() {
// int8 16 32 64 uint
// a := 10
// b := 0b1010 // 表示二进制1010
// c := 037 // 表示八进制37
// d := 0x55 // 表示十六进制55
// e := 0x28p-3 // 表示十六进制22数除以2的3次方(p-3)
// f := 123_56 // 表示12356 _ 分隔数字 为了方便阅读
// fmt.Println(a, b, c, d, e, f)

// a := 20
// fmt.Printf("%d \n", a)
// fmt.Printf("%b \n", a)
// fmt.Printf("%o \n", a)
// fmt.Printf("%x \n", a)

// float32 64
// fmt.Printf("%f \n", math.Pi)
// fmt.Printf("%.2f \n", math.Pi)

// complex64 虚部 实部都是32位
// complex128 虚部 实部都是64位
// c1 := 1 + 2i
// var c1 complex64
// c1 = 1 + 2i
// c2 := 2 + 3i
// var c2 complex128
// c2 = 2 + 3i
// fmt.Println(c1)
// fmt.Println(c2)

// 布尔值默认为false 不允许将整型强转布尔型 不能参与运算 也不能与其他类型进行转换
// var yflag bool
// fmt.Println(yflag)

// 字符串 " " 默认使用UTF-8编码 Print Println Printf
// s1 := "嗨"
// s2 := "Hello World"
// fmt.Printf(s1 + "\n")
// fmt.Printf(s2)
// fmt.Print("test")
// fmt.Println(s1, s2)
// a := 1
// var b bool
// c := true
// if d := 2 > 1;d {
// fmt.Println("test")
// } else {
// fmt.Println("ok")
// }

// 转移符 \r \n \t \' \" \\
// fmt.Println("str := \"d:\\go\\src\"")

// 多行字符串 ` ` 转义符无效 原样输出
// s4 := `
// 今天
// 是个
// 好天气
// `
// fmt.Println(s4)

// 字符串常见操作
// len(str) 求字符串长度
// + 或者 fmt.Sprintf 字符串拼接

// 需要导入strings 包
// strings.Split 分割
// strings.Contains 判断是否包含某字符串 返回布尔值
// HasPrefix 判断是否以字符串开头
// HasSuffix 判断是否以字符串结尾
// Index 字符串出现的位置
// LastIndex 字符串最后出现的位置
// Join 连接数组或切片中元素

// s5 := "Ropon"
// fmt.Println(len(s5))
// fmt.Println("I am " + s5)
// fmt.Println(fmt.Sprintf("I am %s", s5))
// fmt.Println(strings.Split(s5, "o"))
// fmt.Println(strings.Contains(s5, "o"))
// fmt.Println(strings.HasPrefix(s5, "Ro"))
// fmt.Println(strings.HasSuffix(s5, "n"))
// fmt.Println(strings.Index(s5, "o"))
// fmt.Println(strings.LastIndex(s5, "o"))
// s6 := "is Good!"
// fmt.Println(strings.Join([]string{s5, s6}, " "))

// byte rune实际是int32
// 英文字母 1个字节 中文汉字 3个字节
// s1 := "Hello"
// c1 := 'H'
// s2 := "我爱你中国"
// c2 := '我'
// fmt.Println(s1, c1, len(s1))
// fmt.Println(s2, c2, len(s2))

// 遍历字符串
// s1 := "Hello哥哥"
// for i := 0; i < len(s1); i ++ {
// fmt.Printf("%c\n", s1[i])
// }

// for _, r := range s1 {
// fmt.Printf("%c\n", r)
// }

// 修改字符串
// s1 := "niubi"
// 类型转换 T(表达式) T表示需要转换的类型 表达式包括变量 复杂算子 函数返回值
// %T 打印变量类型
// byteA := []byte(s1)
// byteA[0] = 'x'
// fmt.Println(string(byteA))

// s2 := "我爱你"
// runeA := []rune(s2)
// runeA[0] = '你'
// fmt.Println(string(runeA))

// a := 1
// b := 1.25
// s1 := "我们不一样"
// var f bool
// fmt.Printf("%T\n",a)
// fmt.Printf("%T\n",b)
// fmt.Printf("%T\n",s1)
// fmt.Printf("%T\n",f)

// 统计字符串 "Hello我们不一样,每个人都不一样Nb。" 中汉字的数量
s1 := "Hello我们不一样,每个人都不一样Nb。"
byteA := []byte(s1)
runeA := []rune(s1)
fmt.Println(byteA)
fmt.Println(runeA)


}