Created
July 4, 2014 17:23
-
-
Save tfogal/0ce7beffd309156b1cb6 to your computer and use it in GitHub Desktop.
how to do callbacks in Go. implements a 'filter' function. modified from the web example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#include <stdio.h> | |
// The gateway function | |
int callOnMeGo_cgo(int* in) | |
{ | |
return callOnMeGo(in); | |
} | |
*/ | |
import "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "clibrary.h" | |
int* some_c_func(callback_fcn callback, int* arr, size_t n) | |
{ | |
size_t i, j; | |
int* rv; | |
rv = calloc(n, sizeof(int)); | |
for(i=0, j=0; i < n; ++i) { | |
if(callback(&arr[i])) { | |
rv[j++] = arr[i]; | |
} | |
} | |
return rv; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CLIBRARY_H | |
#define CLIBRARY_H | |
#include <stdlib.h> | |
typedef int (*callback_fcn)(int*); | |
int* some_c_func(callback_fcn, int*, size_t); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#cgo CFLAGS: -I . | |
#cgo LDFLAGS: -L . -lclibrary | |
#include "clibrary.h" | |
int callOnMeGo_cgo(int in); // Forward declaration. | |
static inline void c_assign(int* x, int i) { x[i] = i; } | |
static inline int c_lookup(int* x, size_t idx) { return x[idx]; } | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"unsafe" | |
) | |
//export callOnMeGo | |
func callOnMeGo(in *C.int) int { | |
fmt.Printf("Go.callOnMeGo(): called with arg = %d\n", (*in)) | |
if (*in) > 4 { | |
return 1 | |
} | |
return 0 | |
} | |
func main() { | |
fmt.Printf("Go.main(): calling C function with callback to us.\n") | |
cb := (C.callback_fcn)(unsafe.Pointer(C.callOnMeGo_cgo)) | |
arr := (*C.int)(C.malloc(10*4)) | |
for i:=0; i < 10; i++ { C.c_assign(arr, (C.int)(i)) } | |
lst := C.some_c_func(cb, arr, 10) | |
for i:=0; i < 10; i++ { | |
fmt.Printf("%d: %d\n", i, C.c_lookup(lst, (C.size_t)(i))) | |
} | |
C.free(unsafe.Pointer(arr)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
gcc -Wall -Wextra -c clibrary.c | |
ar cru libclibrary.a clibrary.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment