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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package test

import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"go.uber.org/zap/zapcore"
"io"
"log"
"math/rand"
"net/http"
"os"
"sort"
"strings"
"testing"
"time"
"v1/go-import-template/middleware"
"v1/go-import-template/model/request"
)

func TestZap(t *testing.T) {
fmt.Printf("DebugLevel: %v\n", zapcore.DebugLevel)
fmt.Printf("InfoLevel: %v\n", zapcore.InfoLevel)
fmt.Printf("WarnLevel: %v\n", zapcore.WarnLevel)
fmt.Printf("-1: %v\n", zapcore.Level(-1))
fmt.Printf("0: %v\n", zapcore.Level(0))
fmt.Printf("1: %v\n", zapcore.Level(1))
fmt.Printf("2: %v\n", zapcore.Level(2))
}

func TestGetRequests(t *testing.T) {
req, err := http.NewRequest("GET", "https://www.example.com", nil)
client := &http.Client{
Timeout: 1 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println(err)
}
}(resp.Body)
res, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
fmt.Println(string(res))
}

func TestPostRequests(t *testing.T) {
body := map[string]interface{}{
"name": "morpheus",
"job": "leader",
}
jsonData, err := json.Marshal(body)
resp, err := http.Post("https://reqres.in/api/users", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println(err)
}
}(resp.Body)
res, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
resJson := map[string]interface{}{}
err = json.Unmarshal(res, &resJson)
if err != nil {
return
}
fmt.Println(resJson)
}

func TestDeleteRequests(t *testing.T) {
client := &http.Client{}
req, err := http.NewRequest("DELETE", "https://reqres.in/api/users/2", nil)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println(err)
}
}(resp.Body)
res, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
fmt.Println(string(res))
}

func TestPutRequests(t *testing.T) {
client := &http.Client{}
body := map[string]interface{}{
"name": "morpheus",
"job": "zion resident",
}
bodyJson, err := json.Marshal(body)
if err != nil {
return
}
req, err := http.NewRequest("PUT", "https://reqres.in/api/users/2", bytes.NewBuffer(bodyJson))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println(err)
}
}(resp.Body)
res, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
fmt.Println(string(res))
}

func TestMd5(t *testing.T) {
str := "www.topgoer.com"

//方法一
data := []byte(str)
has := md5.Sum(data)

//将[]byte转成16进制
md5str1 := fmt.Sprintf("%x", has)

fmt.Println(md5str1)
}

func TestJwt(t *testing.T) {
token, err := middleware.CreateToken(1)
if err != nil {
return
}
fmt.Println(token)
var res *request.UserClaims
res, err = middleware.ParseToken(token)
if err != nil {
return
}
fmt.Println(res.Uid)
}

// worker 是一个模拟的工作函数,接收任务并处理
func worker(id int, ch chan int) {
for task := range ch {
fmt.Printf("Worker %d processing task %d\n", id, task)
time.Sleep(time.Second) // 模拟处理时间
}
}

func TestWorkers(t *testing.T) {
const numWorkers = 3
const numTasks = 5

// 创建一个任务通道
taskChannel := make(chan int, numTasks)

// 启动多个 worker goroutines
for i := 1; i <= numWorkers; i++ {
go worker(i, taskChannel)
}

// 发送任务到任务通道
for i := 1; i <= numTasks; i++ {
taskChannel <- i
}

// 关闭任务通道,通知 worker 结束工作
close(taskChannel)

// 等待所有 worker 完成工作
time.Sleep(3 * time.Second)
fmt.Println("All tasks are processed")
}

func TestRangeMap(t *testing.T) {
personalityTypes := map[string]string{
"ISTJ": "The Inspector",
"ISTP": "The Crafter",
"ISFJ": "The Protector",
"ISFP": "The Artist",
"INTJ": "The Architect",
"INTP": "The Thinker",
}
for k, v := range personalityTypes {
fmt.Printf("%s: %s\n", k, v)
}
}

func TestDevidingLine(t *testing.T) {
word1 := fmt.Sprintf("fmt.Sprintf: %s", "test01")
word2 := strings.ToUpper(word1)
word3 := strings.ToLower(word1)
word4 := strings.ToTitle(word1)
words := []string{word1, word2, word3, word4}
fmt.Println(strings.Join(words, ", "))
fmt.Println(words[rand.Intn(2)])
fmt.Println(word1[1:3])
}

func TestJsonEncodeDecode(t *testing.T) {
type Person struct {
Name string
Age int
}
p := Person{"John", 25}

// 序列化
b, err := json.Marshal(p)
if err != nil {
return
}
fmt.Println(string(b))

// 反序列化
var p2 Person
err = json.Unmarshal(b, &p2)
if err != nil {
return
}
fmt.Println(p2)
}

func TestLog(t *testing.T) {
log.Println("This is a log message")
log.Printf("This is a log message with %s\n", "formatting")
}

func TestStudyList(t *testing.T) {
s := []int{1, 8, 8, 82, 3, 4, 5, 9.0}
// 定义新的反转切片
var reverseOfS []int

// 遍历原切片 s
for i := range s {
reverseOfS = append(reverseOfS, s[len(s)-1-i])
}

s2 := make([]int, len(s))
copy(s2, s)
sort.Ints(s2)

// 使用切片不会像python一样重新创建地址空间存放数据,所以对切片的操作会影响原切片
s3 := s[:]
sort.Slice(s3, func(i, j int) bool {
return s3[i] < s3[j]
})

fmt.Println(reverseOfS)
fmt.Println(s)
fmt.Println(s2)
fmt.Println(s3)
}

func TestStudyMap(t *testing.T) {
m := map[string]int{
"apple": 5,
"banana": 8,
"grape": 3,
"orange": 9,
}

keys := make([]string, 0, len(m))
for k, v := range m {
fmt.Printf("%s -> %d\n", k, v)
keys = append(keys, k)
}

fmt.Println(keys)
fmt.Println(m)
}

func TestChannel(t *testing.T) {
// 有缓冲的通道
ch := make(chan int, 1)
ch <- 1
fmt.Println(<-ch)

// 无缓冲的通道
ch2 := make(chan int)
go func() {
ch2 <- 1
}()
fmt.Println(<-ch2)
}

func TestFileOperate(t *testing.T) {
// =========== 写入文件 ===========
fileName := "test1.txt"
strTest := "测试测试"
var d = []byte(strTest)
err := os.WriteFile(fileName, d, 0666)
if err != nil {
fmt.Println("write fail")
return
}
fmt.Println("write success")

// =========== 读取文件 ===========
f, err := os.ReadFile("test1.txt")
if err != nil {
fmt.Println("read fail", err)
}
fmt.Println(string(f))
}

func TestStruct(t *testing.T) {
type Person struct {
Name string
Age int
}
p := Person{"John", 25}
fmt.Println(p)
}

// 定义接口 I
type I interface {
String() string
}

// 定义 MyType 结构体
type MyType struct {
value string
}

// 实现接口 I 的 String 方法
func (i MyType) String() string {
return i.value
}

// 测试接口的函数
func TestInterface(t *testing.T) {
var i I
i = MyType{"hello"}

// 直接使用接口变量调用方法
fmt.Println(i.String()) // 输出: hello
}

func TestTime(t *testing.T) {
now := time.Now()
log.Println("时间戳(秒):", now.Unix()) // 输出:时间戳(秒) : 1665807442
log.Println("时间戳(毫秒):", now.UnixMilli()) // 输出:时间戳(毫秒): 1665807442207
log.Println("时间戳(微秒):", now.UnixMicro()) // 输出:时间戳(微秒): 1665807442207974
log.Println("时间戳(纳秒):", now.UnixNano()) // 输出:时间戳(纳秒): 1665807442207974500

date := time.DateTime
log.Println("当前时间:", date)
}

func TestQiyeweixin(t *testing.T) {
webhook := ""
body := map[string]interface{}{
"msgtype": "markdown",
"markdown": map[string]string{
"content": "test01",
},
}
jsonData, err := json.Marshal(body)
if err != nil {
fmt.Println(err)
return
}
newRequest, err := http.NewRequest("POST", webhook, bytes.NewBuffer(jsonData))
if err != nil {
return
}
client := &http.Client{
Timeout: 1 * time.Second,
}
resp, err := client.Do(newRequest)
if err != nil {
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println(err)
}
}(resp.Body)
res, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
fmt.Println(string(res))
}