-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
80 lines (58 loc) · 1.39 KB
/
convert.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
package goutils
import (
"fmt"
"time"
"strconv"
"encoding/json"
"github.com/mitchellh/mapstructure"
)
const (
date_layoutUS = "January 2, 2006"
)
// Converters //
func To_String(T interface{}) (string) {
switch T.(type) {
case int:
return strconv.Itoa(T.(int))
case int64:
return strconv.FormatInt(T.(int64), 10)
case bool:
return strconv.FormatBool(T.(bool))
case float64:
return fmt.Sprintf("%g", T.(float64))
default:
return T.(string)
}
}
func To_Int(T interface{}) (int) {
switch T.(type) {
case string:
intVar, _ := strconv.Atoi(T.(string))
return intVar
case float64:
return int(T.(float64))
default:
return T.(int)
}
}
func To_Float64(T interface{}) (float64) {
switch T.(type) {
case string:
float64Var, _ := strconv.ParseFloat(T.(string), 64)
return float64Var
case int:
return float64(T.(int))
default:
return T.(float64)
}
}
func String_To_Date(date_str string) (time.Time) {
date, _ := time.Parse(date_layoutUS, date_str)
return date
}
func To_Struct(inter interface{}, struc interface{}) {
mapstructure.Decode(inter, struc)
}
func Bytes_To_Json(byteValue []byte, json_data interface{}) {
json.Unmarshal(byteValue, json_data)
}