正在显示
2 个修改的文件
包含
45 行增加
和
0 行删除
1 | package common | 1 | package common |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "bytes" | ||
4 | "fmt" | 5 | "fmt" |
5 | "encoding/json" | 6 | "encoding/json" |
7 | + "math/rand" | ||
6 | ) | 8 | ) |
7 | 9 | ||
8 | // Must panics if err is not nil. | 10 | // Must panics if err is not nil. |
@@ -33,4 +35,17 @@ func AssertJson(object interface{})string{ | @@ -33,4 +35,17 @@ func AssertJson(object interface{})string{ | ||
33 | return "" | 35 | return "" |
34 | } | 36 | } |
35 | return string(json) | 37 | return string(json) |
38 | +} | ||
39 | + | ||
40 | +var randomChars = "ABCDEFGHJKMNPQRSTWXYZabcdefhjkmnprstwxyz2345678" /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ | ||
41 | +func RandomString(l int)string{ | ||
42 | + if l<=0{ | ||
43 | + return "" | ||
44 | + } | ||
45 | + lenChars :=len(randomChars) -1 | ||
46 | + rsp :=bytes.NewBuffer(nil) | ||
47 | + for i:=0;i<l;i++{ | ||
48 | + rsp.WriteByte(randomChars[rand.Intn(lenChars)]) | ||
49 | + } | ||
50 | + return rsp.String() | ||
36 | } | 51 | } |
common/common_test.go
0 → 100644
1 | +package common | ||
2 | + | ||
3 | +import ( | ||
4 | + "testing" | ||
5 | +) | ||
6 | + | ||
7 | +func Test_RandomString(t *testing.T){ | ||
8 | + input :=[]int{6,10,20,16,32} | ||
9 | + for i:=range input{ | ||
10 | + l :=input[i] | ||
11 | + out := RandomString(l) | ||
12 | + if len(out)!=l{ | ||
13 | + t.Fatal("length not equal want :",l," out:",out) | ||
14 | + } | ||
15 | + } | ||
16 | +} | ||
17 | + | ||
18 | +func Benchmark_RandomString(b *testing.B) { | ||
19 | + input :=[]int{10,20,16,32} | ||
20 | + l :=0 | ||
21 | + out :="" | ||
22 | + for i:=0;i<b.N;i++{ | ||
23 | + l=i%4 | ||
24 | + l=input[l] | ||
25 | + out = RandomString(l) | ||
26 | + if len(out)!=l{ | ||
27 | + b.Fatal("length not equal want :",l," out:",out) | ||
28 | + } | ||
29 | + } | ||
30 | +} |
-
请 注册 或 登录 后发表评论