-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
214 lines (192 loc) · 4.43 KB
/
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
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
package main
/*
int sum(int a, int b) { return a+b; };
int abs(int a){
if (a <0) return -a ;
else return a;
};
*/
import "C"
import (
"crypto/md5"
"fmt"
"io"
"sort"
"sync"
"time"
Ca "github.com/eric-jxl/go/gocache"
unit "github.com/eric-jxl/go/test/unittest"
)
const (
name = "Eric"
// DefaultExpiration _ = iota
//KB = 1 << (10 * iota)
//MB = 1 << (10 * iota)
//GB = 1 << (10 * iota)
//TB = 1 << (10 * iota)
//PB = 1 << (10 * iota)
//x = iota // x == 0
//y = iota // y == 1
//z = iota // z == 2
//w
DefaultExpiration time.Duration = 0
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Addr string `json:"addr,omitempty"`
}
//New returns a new hash.Hash computing the MD5 checksum. The Hash also
//implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to
//marshal and unmarshal the internal state of the hash.
func md5encrypt() {
//假设用户名 abc,密码 123456
h := md5.New()
var pwd string
fmt.Println("输入密码:")
_, err := fmt.Scanln(&pwd)
if err != nil {
return
}
_, err = io.WriteString(h, pwd)
if err != nil {
return
}
// pwmd5 等于 e10adc3949ba59abbe56e057f20f883e
pwmd5 := fmt.Sprintf("%x", h.Sum(nil))
// 指定两个 salt: salt1 = @#$% salt2 = ^&*()
salt1 := "@#$%"
salt2 := "^&*()"
// salt1 + 用户名 + salt2 + MD5 拼接
_, _ = io.WriteString(h, salt1)
_, _ = io.WriteString(h, "abc")
_, _ = io.WriteString(h, salt2)
_, _ = io.WriteString(h, pwmd5)
last := fmt.Sprintf("%x", h.Sum(nil))
fmt.Println(last)
}
func setCache() {
tc := Ca.New(DefaultExpiration, 1)
s := struct {
name string
age int
}{"nike", 11}
tc.Set("a", s, 0)
a, ok := tc.Get("a")
if ok {
fmt.Println(a)
} else {
fmt.Println("Key is not Exists!")
}
if err := tc.Increment("tint8", 2); err != nil {
e := fmt.Errorf("implementError:(%s) ", err)
fmt.Println(e.Error())
}
}
func routine() {
wg := sync.WaitGroup{}
var mutex sync.Mutex
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
mutex.Lock()
//fmt.Printf("第%d次 Locked\n", i)
setCache()
//time.Sleep(1 * time.Second)
//fmt.Print("UnLock\n")
mutex.Unlock()
wg.Done()
}()
}
wg.Wait()
}
func producer(c chan<- int) {
for {
time.Sleep(1 * time.Second)
c <- 1
}
}
func consumer(c <-chan int) {
for {
fmt.Println(<-c)
}
}
func testCase() {
c := make(chan int, 10)
go producer(c)
go consumer(c)
select {}
}
func fibTest(a ...interface{}) {
start := time.Now()
result := unit.Fib(10)
end := time.Now()
timeDiff := end.Sub(start).Seconds()
fmt.Printf("斐切拉波数列第40项的值为:%d,用时:%.2f \n", result, timeDiff)
h := time.Now()
unit.SyncMux()
fmt.Printf("APi调用时长%.2f\n", time.Since(h).Seconds())
}
func main() {
//c := [...]int{4, 5, 6}
//d := `hello
//world`
//fmt.Printf("%v,%v,%s,%d", name, c, d, len(c))
//
//slice := []byte{'a', 'b', 'c', 'd'}
//fmt.Println(len(slice),string(slice))
//fmt.Println(KB);fmt.Println(MB);fmt.Println(GB);fmt.Println(TB);fmt.Println(PB)
//md5encrypt()
//go routine()
//testCase()
//md5encrypt()
//fmt.Println(C.sum(1, 2))
//fmt.Println(C.abs(-2))
//fibTest()
//pt := new(Person)
////s := make(chan int,1)
////s <- 1
//str := make([]string, 0, 10)
//str = append(str, "22222", "2355", name)
//fmt.Printf("%+v\n", str)
//ptr := unsafe.Sizeof(str)
//pts := unsafe.Alignof(str)
//fmt.Println(ptr, pts)
//pt.Name, pt.Age = name, 17
//
//obstruct := make([]string, len(str), 10)
//copy(obstruct, str)
//fmt.Println(reflect.DeepEqual(obstruct, str))
//fmt.Println(obstruct)
//fmt.Println(unit.ConnectMysql())
//unit.CpuProfile()
//unit.BufferWFile("Eric\n","a.txt")
//var ct = []string{"AAAAA\n","AAAAA\n","AAAAA\n"}
//unit.BufferWFile(ct,"a.txt")
unit.GetIp()
source := []string{"Apple","Orange","Plum","Banana","Grape"}
sort.Strings(source)
slice := source[2:3:3]
slice= append( slice,"Kiwi")
//fmt.Println(append(source,slice...))
//fmt.Println(removeDuplicateElement(source))
array :=[3] *string{new(string),new(string),new(string)}
*array[0] = "Red"
fmt.Println(*array[0])
a := make(chan int,10)
a <- 10
defer close(a)
fmt.Printf("%v\n",<-a)
}
//removeDuplicateElement:删除切片重复元素
func removeDuplicateElement(elements []string) []string {
result := make([]string, 0, len(elements))
temp := map[string]struct{}{}
for _, item := range elements {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}