-
Notifications
You must be signed in to change notification settings - Fork 1
/
b.go
56 lines (45 loc) · 777 Bytes
/
b.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
package main
/*
typedef struct{
int n;
_GoString_ s;
}TestObj;
static inline void* call(void *a, void *b) {return ((void* (*)(void*))a)(b);}
*/
import "C"
import (
crypto_md5 "crypto/md5"
"fmt"
"io"
"unsafe"
)
type GoObj C.TestObj
//export sum
func sum(a, b int) int {
return a + b
}
//export md5
func md5(o GoObj) *C.char {
h := crypto_md5.New()
io.WriteString(h, o.s)
s := fmt.Sprintf("%x", h.Sum(nil))
return C.CString(s)
}
//export fibonacci
func fibonacci(o GoObj) int {
n := uint64(o.n - 1)
if o.n <= 1 {
return int(n)
}
var n2, n1 uint64 = 0, 1
for i := uint64(2); i < n; i++ {
n2, n1 = n1, n1+n2
}
return int(n2 + n1)
}
//export callback
func callback(a, b unsafe.Pointer) unsafe.Pointer {
r := C.call(a, b)
return r
}
func main() {}