/
efaceconv.go
50 lines (42 loc) · 1.17 KB
/
efaceconv.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
package ecutils
import "unsafe"
var strEface interface{}
var strKind uintptr
var sbEface interface{}
var sbKind uintptr
func init() {
strEface = interface{}("")
strKind = GetKind(strEface)
sbEface = interface{}([]byte{})
sbKind = GetKind(sbEface)
}
// GetKind returns arg's kind
// panics if arg is a pointer to value
func GetKind(arg interface{}) uintptr {
return *(*uintptr)((*[2]unsafe.Pointer)(unsafe.Pointer(&arg))[0])
}
// GetDataPtr returns pointer to arg's data
// panics if arg is a pointer to value
func GetDataPtr(arg interface{}) unsafe.Pointer {
return ((*[2]unsafe.Pointer)(unsafe.Pointer(&arg))[1])
}
// Eface2String returns pointer to string and true if arg is a string
// or nil and false otherwise
//
// DEPRECATED: use go generate
func Eface2String(arg interface{}) (*string, bool) {
if GetKind(arg) == strKind {
return (*string)(GetDataPtr(arg)), true
}
return nil, false
}
// Eface2ByteSlice returns pointer to []byte and true if arg is a []byte
// or nil and false otherwise
//
// DEPRECATED: use go generate
func Eface2ByteSlice(arg interface{}) (*[]byte, bool) {
if GetKind(arg) == sbKind {
return (*[]byte)(GetDataPtr(arg)), true
}
return nil, false
}