forked from appleboy/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
68 lines (57 loc) · 1.24 KB
/
string.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
package tools
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"strconv"
"time"
)
func StringToInt64(e string) (int64, error) {
return strconv.ParseInt(e, 10, 64)
}
func StringToInt(e string) (int, error) {
return strconv.Atoi(e)
}
func GetCurrntTimeStr() string {
return time.Now().Format("2006/01/02 15:04:05")
}
func GetCurrntTime() time.Time {
return time.Now()
}
func StructToJsonStr(e interface{}) (string, error) {
if b, err := json.Marshal(e); err == nil {
return string(b), err
} else {
return "", err
}
}
func GetBodyString(c *gin.Context) (string, error) {
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
fmt.Printf("read body err, %v\n", err)
return string(body), nil
} else {
return "", err
}
}
func JsonStrToMap(e string) (map[string]interface{}, error) {
var dict map[string]interface{}
if err := json.Unmarshal([]byte(e), &dict); err == nil {
return dict, err
} else {
return nil, err
}
}
func StructToMap(data interface{}) (map[string]interface{}, error) {
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
mapData := make(map[string]interface{})
err = json.Unmarshal(dataBytes, &mapData)
if err != nil {
return nil, err
}
return mapData, nil
}