-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
169 lines (155 loc) · 3.13 KB
/
helper.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
package helper
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"reflect"
"strings"
"time"
)
func Json(data interface{}) string {
tmp, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "json_encode error"
}
return string(tmp)
}
//获取时间戳 单位微秒
func Time() int64 {
return time.Now().UnixNano() / 1000000
}
//延迟毫秒
func Sleep(m time.Duration) {
time.Sleep(m * time.Second / 1000)
}
//空函数
func Nullf(d interface{}) {}
//打印数据
func Print(data interface{}) {
t := reflect.TypeOf(data)
v := reflect.ValueOf(data)
if k := t.Kind(); k != reflect.Struct {
fmt.Println(t.Name())
fmt.Println(v)
return
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
fmt.Println(f)
}
fmt.Println(v)
}
//生成md5
func Md5(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
//随机字符串
func RandomString(length int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM"
bytes := []byte(str)
slen := len(bytes)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ {
result = append(result, bytes[r.Intn(slen)])
}
return string(result)
}
//读取文件
func ReadFile(fileName string) ([]byte, error) {
f, err := os.Open(fileName)
if err != nil {
return []byte{}, err
}
defer f.Close()
fd, err := ioutil.ReadAll(f)
return fd, err
}
//写入文件
func WriteFile(fileName string, data []byte) error {
//创建目录
if err := os.MkdirAll(filepath.Dir(fileName)+`/`, 0666); err != nil {
return err
}
//写入文件
err := ioutil.WriteFile(fileName, data, 0666)
return err
}
//追加写入文件
func WriteFileAppend(fileName string, data []byte) error {
var f *os.File
var err error
//创建目录
if err = os.MkdirAll(filepath.Dir(fileName), 0666); err != nil {
return err
}
if _, err := os.Stat(fileName); err == nil { //文件存在
f, err = os.OpenFile(fileName, os.O_APPEND, 0666) //打开文件
} else { //文件不存在
f, err = os.Create(fileName) //创建文件
}
if err != nil {
return err
}
//将文件写进去
_, err = f.Write(data) //追加
return err
}
//获取byte中某个字符的索引
func ByteIndex(arr []byte, s byte) int {
for i, v := range arr {
if v == s {
return i
}
}
return -1
}
//读取key=value类型的配置文件
func GetConfig(path string) (map[string]string, error) {
config := make(map[string]string)
f, err := os.Open(path)
defer f.Close()
if err != nil {
return config, err
}
r := bufio.NewReader(f)
for {
b, _, err := r.ReadLine()
if err != nil && err == io.EOF {
break
}
s := strings.TrimSpace(string(b))
index := strings.Index(s, "=")
if index < 0 {
continue
}
key := strings.TrimSpace(s[:index])
if len(key) == 0 {
continue
}
value := strings.TrimSpace(s[index+1:])
if len(value) == 0 {
continue
}
config[key] = value
}
return config, nil
}
//拼接byte
func ByteJoin(b ...[]byte) []byte {
var buf bytes.Buffer
for _, v := range b {
buf.Write(v)
}
return buf.Bytes()
}