RosettaCodeData/Task/Use-another-language-to-call-a-function/Go/use-another-language-to-call-a-function-2.go
Ingy döt Net 68f8f3e56b all tasks
2013-04-11 01:07:29 -07:00

27 lines
500 B
Go

package main
// #include <stdlib.h>
// extern void Run();
import "C"
import "unsafe"
func main() {
C.Run()
}
const msg = "Here am I"
//export Query
func Query(cbuf *C.char, csiz *C.size_t) C.int {
if int(*csiz) <= len(msg) {
return 0
}
pbuf := uintptr(unsafe.Pointer(cbuf))
for i := 0; i < len(msg); i++ {
*((*byte)(unsafe.Pointer(pbuf))) = msg[i]
pbuf++
}
*((*byte)(unsafe.Pointer(pbuf))) = 0
*csiz = C.size_t(len(msg) + 1)
return 1
}