forked from swxctx/gutil
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbytes.go
More file actions
37 lines (32 loc) · 733 Bytes
/
bytes.go
File metadata and controls
37 lines (32 loc) · 733 Bytes
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
package gutil
import (
"encoding/json"
"unsafe"
)
// StringToBytes
func StringToBytes(s string) []byte {
sp := *(*[2]uintptr)(unsafe.Pointer(&s))
bp := [3]uintptr{sp[0], sp[1], sp[1]}
return *(*[]byte)(unsafe.Pointer(&bp))
}
// BytesToString []byte to string
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// InterfaceToBytes
func InterfaceToBytes(data interface{}) ([]byte, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
return b, nil
}
// BytesToMapInterface
func BytesToMapInterface(data []byte) (map[string]interface{}, error) {
m := map[string]interface{}{}
err := json.Unmarshal([]byte(data), &m)
if err != nil {
return nil, err
}
return m, nil
}