-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
101 lines (81 loc) · 2.55 KB
/
helper_test.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
package gosupport
import (
"errors"
"fmt"
"testing"
"time"
)
func TestIntSum(t *testing.T) {
total := IntSum(1, 3, 5)
fmt.Println(total) //9
//切片单元值求和
nums := []int{1, 2, 3, 4}
fmt.Println(IntSum(nums...)) //10
}
func TestToJson(t *testing.T) {
type UserInfo struct {
Userid int `json:"userid"`
Nickname string `json:"nickname"`
Age int `json:"age"`
}
//{"userid":123,"nickname":"张三","age":28}
fmt.Println(ToJson(&UserInfo{Userid: 123, Age: 28, Nickname: "张三"}))
}
func TestInitStruct4DefaultTag(t *testing.T) {
config := &struct {
B bool `default:"true"` //true
B2 bool `default:"false"` //false
S string `default:"默认字符串哈哈" json:"s"`
S2 string
I int `default:"10"` //10
I2 int8 `default:"7"` //7
I3 int16 `default:"160"` //160
I4 int32 `default:"320"`
I5 int64 `default:"640"`
F float64 `default:"9.8"`
F2 float32 `default:"3.89"`
T time.Duration `default:"1000000"` //1ms
E int `default:""` //0
E2 int `default:"0"` //0
U uint `default:"11"` //11
U2 uint8 `default:"22"` //22
}{}
InitStruct4DefaultTag(config)
fmt.Println(fmt.Sprintf("%+v", config))
}
// go test -run="TestNewSequence"
func TestNewSequence(t *testing.T) {
ret := NewSequence().SetPrefix("SO").SetStepSize(108).SetUid(10010).GetSeq()
fmt.Println(ret)
ret2 := NewSequence().SetPrefix("SO").SetStepSize(-1).SetUid(10010).GetSeq()
fmt.Println(ret2)
uidMod1 := SeqString(ret2).GetUidMod4Seq()
ret3 := NewSequence().SetPrefix("SOL").SetStepSize(108).GetSeq(uidMod1)
fmt.Println(ret3)
var uidMod int64 = 789
ret4 := NewSequence().SetPrefix("OPL").SetStepSize(108).GetSeq(uidMod)
fmt.Println(ret4)
}
// go test -run="TestNewWrapError"
func TestNewWrapError(t *testing.T) {
we := NewWrapError()
_ = we.AddError(errors.New("错误信息1"))
_ = we.AddError(errors.New("错误信息2"))
_ = we.AddError(errors.New("错误信息3"))
fmt.Println(we.Error.Error()) //错误信息1; 错误信息2; 错误信息3
}
// go test -run=TestIsStrMaper
func TestIsStrMaper(t *testing.T) {
m1 := Metadata{"hello": "world", "username": "tom"}
m1.Set("city", "shanghai")
fmt.Println(m1) // map[city:shanghai hello:world username:tom]
fmt.Println(IsStrMaper(m1)) // true
fmt.Println(IsStrMaper("he")) // false
}
// go test -run=TestMatchEnvStr
func TestMatchEnvStr(t *testing.T) {
str := "APP_ENV = prod "
key, val := MatchEnvStr(str)
fmt.Println("key=", key)
fmt.Println("val=", val)
}