如何从C回调函数

时间:2018-01-28 21:46:30

标签: go cgo

我试图将C库合并到我的Go应用程序中。 C库的接口使用回调来处理日志记录。最终看起来像这样:

/*
#include "clibrary/header.h"

void log_callback(const char * log)
{
  /* I'd like to get this log value to use in my Go code */
  fprintf(stderr, "Log is: %s\n", log);
}

/* Using this wrapper method so the callback can be added to
   the function call since it seems from my reading that 
   access to a C pointer to a function is difficult in CGO. 
*/    
int do_c_stuff_wrapper(Context * context)
{

  return do_c_stuff(context, log_callback)
}

*/
import "C"

import "fmt"

func DoCStuff() {

  c_context := C.init_context()
  ret := C.do_c_stuff_wrapper(c_context) 
  fmt.Printf("The return value is %v\n", ret)

}

我希望从回调中获取日志值"返回" Go代码,所以我可以使用它,我在如何做到这一点上空白。

0 个答案:

没有答案