Could this be somehow used with golang?
s3rj1k opened this issue · 6 comments
Have you ever tried to expose golang function as interfaces, and using C program as a proxy call? It should be reasonable.
References: Using shared Go library in C
http://blog.ralch.com/tutorial/golang-sharing-libraries/
Hi s3rj1k,
I am not familiar with Go lang, but i just try to do a simple compilation and it's working fine.
package main
import "C"
//export go_init
func go_init(ctx interface{}) {
// TODO do your initialization
}
//export go_exit
func go_exit(ctx interface{}) {
// TODO do your exit section
}
func main() {
}built from command "go build -buildmode=c-archive -o my_go.a my_go.go"
#include <stdio.h>
#include <stdio.h>
#include "my_go.h"
#include <ngx_http_c_func_module.h>
void ngx_http_c_func_init(ngx_http_c_func_ctx_t *ctx) {
ngx_http_c_func_log_info(ctx , "Initiazing GoLang Application");
GoInterface i;
i.v = ctx;
go_init(i);
}
void ngx_http_c_func_exit(ngx_http_c_func_ctx_t *ctx) {
ngx_http_c_func_log_info(ctx , "Exiting GoLang Application");
GoInterface i;
i.v = ctx;
go_exit(i);
}built from command "gcc -shared -o libgotest.so -fPIC my_cgo.c my_go.a"
@Taymindis Wow, that looks super.
How can I know witch functions i can use inside golang?
Can I please point to additional docs?
@s3rj1k You have to expose your c function to let your go program to include your c function header file, I am not sure how go call back to c as I am not familiar with this golang.
For instance, this is simple draft of how it call back to c function from go
// your .h file
extern void my_app_log_info(GoInterface p);// your .c file
void my_app_log_info(GoInterface p) {
ngx_http_c_func_log(info, p.v , "Hello from Go program");
}//your .go file
// #include <your_header_file>
import "C"
//export go_init
func go_init(ctx interface{}) {
C.my_app_log_info(ctx);
}
Hi s3rj1k,
This is the simple SOP of how to implement Go Lang Application.
file: my_go.go
package main
// #cgo CFLAGS: -I/usr/local/include
// #cgo LDFLAGS: -Wl,--unresolved-symbols=ignore-all
/*
#include <ngx_http_c_func_module.h>
#include <string.h>
typedef void* ptr;
static inline void ngx_go_log(void *ctx,const char* logMessage) {
ngx_http_c_func_log(info, ctx , "strlen %d", strlen(logMessage));
ngx_http_c_func_log(info, ctx , "%s", logMessage);
}
*/
import "C"
import "unsafe"
func main() {}
//export returnSomeInt
func returnSomeInt(ctx C.ptr) int {
var log_msg string= "Logging from GO\000"
C.ngx_go_log(unsafe.Pointer(ctx), C._GoStringPtr(log_msg))
return 5
}file: my_go_client.c
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <ngx_http_c_func_module.h>
// define types needed
typedef long long go_int;
typedef double go_float64;
typedef struct{void *arr; go_int len; go_int cap;} go_slice;
typedef struct{const char *p; go_int len;} go_str;
// Go static function
static go_int (*go_returnSomeInt)(void*);
// Go application handle
static void *handle;
void ngx_http_c_func_init(ngx_http_c_func_ctx_t *ctx) {
ngx_http_c_func_log(info, ctx , "Initializing GoLang Application");
char *error;
// use dlopen to load shared object
// handle = dlopen (NULL, RTLD_LAZY | RTLD_NOW | RTLD_GLOBAL);
handle = dlopen ("/etc/nginx/apps/libmy_go.so", RTLD_LAZY | RTLD_NOW);
if (!handle) {
ngx_http_c_func_log(err, ctx, "%s", "unable to initialized the libmy_go.so Application ");
return;
}
// resolve Add symbol and assign to fn ptr
go_returnSomeInt = dlsym(handle, "returnSomeInt");
if ((error = dlerror()) != NULL) {
ngx_http_c_func_log(err, ctx, "%s", error);
return;
}
ngx_http_c_func_log(info, ctx , "Testing Go lang function return 5 == %d ? ", go_returnSomeInt(ctx));
}
void ngx_http_c_func_exit(ngx_http_c_func_ctx_t *ctx) {
ngx_http_c_func_log(info, ctx , "Exiting GoLang Application");
dlclose(handle);
}go build -o libmy_go.so -buildmode=c-shared my_go.go << compile go as shared lib
gcc -shared -o libgotest.so -fPIC my_go_client.c -ldl << compile client as shared lib
mv libmy_go.so libgotest.so /etc/nginx/apps/ mv this 2 file the location where you load the lib
file :: nginx.conf
ngx_http_c_func_link_lib "/etc/nginx/apps/libgotest.so";
Hi s3rj1k,
Since you have no response on this issue. I assumed it's fixed.