-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
43 lines (38 loc) · 1.01 KB
/
utils.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
package gbind
import (
"reflect"
"runtime"
"unsafe"
)
// // ReflectTypeID returns unique type identifier of v.
// func ReflectTypeID(v interface{}) uintptr {
// // rt := reflect.TypeOf(v)
// // return uintptr(unsafe.Pointer(rt))
// return 0
// }
// MethodName get the name of the current executing function
func MethodName() string {
pc, _, _, _ := runtime.Caller(2)
f := runtime.FuncForPC(pc)
if f == nil {
return "unknown method"
}
return f.Name()
}
// SliceToString slice to string with out data copy
func SliceToString(b []byte) (s string) {
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pstring.Data = pbytes.Data
pstring.Len = pbytes.Len
return
}
// StringToSlice string to slice with out data copy
func StringToSlice(s string) (b []byte) {
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pbytes.Data = pstring.Data
pbytes.Len = pstring.Len
pbytes.Cap = pstring.Len
return
}