一.基本函数调用与定义
1 package main
2
3 import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "math/rand"
8 "mylib/pkg/student"
9 "mylib/pkg/utils"
10 "sort"
11 "strconv"
12 "strings"
13 "time"
14 "unsafe"
15 )
16
17 func test1(n1 int32, n2 int32) int32 {
18 return n1 + n2
19 }
20
21 func testPrint() {
22 aa := test1
23 bb := test1
24 fmt.Println("hello Golang!")
25 // utils.SayHello()
26 utils.SayHelloWorld()
27 fmt.Printf("aa类型=%T, test1类型=%T", aa, test1)
28
29 fmt.Printf("aa类型=%d, test1类型=%v, %d", &aa, &bb, unsafe.Sizeof(aa))
30 }
二.字符串处理
1 func testString() {
2 tstr := "hello 中国"
3
4 // 字符串长度: 返回的是字节长度,中文是以UTF-8编码,所以一个中文占用3个字节
5 fmt.Println("str len=", len(tstr)) // 输出12
6
7 // 遍历字符串:这种遍历是按单个字节遍历
8 for i := 0; i < len(tstr); i++ {
9 if i == 0 {
10 fmt.Println("将单个字节转成字符:")
11 }
12 fmt.Printf("字符=%c
", tstr[i]) // 这里将中文拆开的时候会出现乱码
13 }
14
15 // 将字符串切片遍历,这样就不会出现乱码了
16 tstr1 := []rune(tstr)
17 for index, val := range tstr1 {
18 if index == 0 {
19 fmt.Println("将单个字节转成字符:")
20 }
21 fmt.Printf("字符=%c
", val) // 这里将中文拆开的时候会出现乱码
22 }
23
24 // 字符串转整数
25 n, err := strconv.Atoi("123")
26 if err != nil {
27 fmt.Println("转换错误")
28 }
29 fmt.Println("字符串转整数后的结果:", n)
30
31 // 整数转字符串
32 tstr2 := strconv.Itoa(12345)
33 fmt.Printf("str=%v, str type=%T
", tstr2, tstr2)
34
35 // 字符串专程byte
36 var bytes = []byte("hello golang 汉")
37 fmt.Printf("bytes=%v
", bytes)
38
39 // 把byte 转成字符串
40 tstr3 := string(bytes)
41 fmt.Printf("byte 转 str=%v
", tstr3)
42
43 // 10进制转2丶8丶16进制:第二个参数就是你要转换的目标进制(取值:2 8 16)
44 tstr4 := strconv.FormatInt(123, 2)
45 fmt.Printf("十进制转二进制:%v tstr4 type = %T
", tstr4, tstr4)
46
47 // 字符串查询
48 tb := strings.Contains("hello world !", "hello")
49 fmt.Println("查找字符串后的结果:", tb) // true
50
51 // 字符串查询,返回子串出现的次数
52 num1 := strings.Count("hello world !", "l")
53 fmt.Println("查找字符串后的结果:", num1) // 3
54
55 // 字符串比较."=="比较是区分大小写
56 tb1 := strings.EqualFold("Abc", "abc")
57 tb2 := "Abc" == "abc"
58 fmt.Println("字符串比较,不区分大小写:", tb1, "区分大小写比较:", tb2)
59
60 // 查找子字符串最初出现的索引
61 index := strings.Index("Hello world !", "e")
62 fmt.Println("查找字符串第一次出现的索引位置:", index) // 1
63
64 // 查找子字符串最后出现的索引
65 index = strings.LastIndex("Hello world !", "l")
66 fmt.Println("查找子字符串最后出现的索引:", index) // 9
67
68 // 字符串替换:最后一个参数代表替换的次数,-1就替换所有(strings.Replace 这也是替换所有)
69 tstr5 := strings.Replace("hello world !", "l", "oo", 1)
70 fmt.Println("字符串替换:", tstr5) // "heoolo world !"
71
72 // 字符串分割
73 strArr := strings.Split("hello,world,!", ",")
74 fmt.Println("字符串分割:", strArr) // "[hello world !]"
75
76 // 大小写转换:转小写
77 tstr6 := strings.ToLower("Hello")
78 fmt.Println("大小写转换:", tstr6) // "hello"
79
80 // 大小写转换:转大写
81 tstr7 := strings.ToUpper("Hello")
82 fmt.Println("大小写转换:", tstr7) // "HELLO"
83
84 // 去掉左右空格
85 tstr8 := strings.TrimSpace(" Hel lo ")
86 fmt.Println("去掉左右空格:", tstr8) // "Hel lo"
87
88 // 去掉字符串左右两边指定的字符:去掉感叹号与空格
89 tstr9 := strings.Trim("! Hel ! lo !", "! ")
90 fmt.Println("去掉字符串左右两边指定的字符:", tstr9) // "Hel ! lo"
91
92 // 判断字符串开头
93 fmt.Println("判断字符串开头:", strings.HasPrefix("ftp://192.169.1.2", "ftp")) // true
94
95 // 判断字符串结尾
96 fmt.Println("判断字符串结尾:", strings.HasSuffix("ftp://192.169.1.2", ".2")) // true
97
98 // 字符串拼接:浮点数四舍五入
99 tstr10 := fmt.Sprintf("int=%d, float=%.2f", 1, 3.145)
100 fmt.Println("字符串拼接:", tstr10)
101 // join 来字符串拼接
102 tstr11 := strings.Join([]string{"中国", "深圳"}, "@")
103 fmt.Println("join 来字符串拼接:", tstr11)
104 // strings.Builder来拼接:效率最高
105 var bt strings.Builder
106 bt.WriteString("中国")
107 bt.WriteString("@")
108 bt.WriteString("武汉")
109 fmt.Println("strings.Builder 来字符串拼接:", bt.String())
110
111 }
三.Json处理操作
1 func testJson() {
2 type Student struct {
3 Name string `json:"nametag"`
4 Age int16
5 Gender bool
6 }
7 type Class struct {
8 Id string
9 Students []Student
10 }
11 s := Student{"张三", 18, true}
12 c := Class{
13 Id: "七年级(2)班",
14 Students: []Student{s, s, s},
15 }
16 // 结构体转json串
17 bytes, err := json.Marshal(c)
18 if err != nil {
19 fmt.Println("json序列化失败", err)
20 return
21 }
22 jsonStr := string(bytes)
23 fmt.Println("结构体转json串:", jsonStr)
24
25 // 字符串转结构体
26 var c2 Class
27 err = json.Unmarshal([]byte(jsonStr), &c2)
28 fmt.Printf("字符串转结构体:%v
", c2)
29
30 // 也可以使用第三方的库,会更快一点:github.com/bytedance/sonic 只需要将上面代码中的“json”替换成“sonic”
31
32 }