package main /* #cgo LDFLAGS: -ldl #include #include #include #include void* load_so(char* path) { void* handle = dlopen(path, RTLD_LAZY); if (!handle) { fprintf(stderr, "failed to open so:%s, error:%d\n", path, errno); return NULL; } printf("succeeded to load so:%s\n", path); return handle; } int so_incr(void* handle, int i) { typedef int (*p_inc)(int i); p_inc f = (p_inc)dlsym(handle, "printlog"); return f(i); } void release_so(void* handle) { dlclose(handle); } */ import "C" import ( "fmt" "io" "os" "unsafe" "embed" ) //go:embed static var local embed.FS func main() { in, err := local.Open("static/libs/libtest.so") if nil != err { fmt.Println("failed to read file", err) } else { fmt.Println("load dll okay") } path := "./embed-" + "libtest.so" out, err := os.Create(path) if nil != err { fmt.Println("create backup failed") } _, err = io.Copy(out, in) _ = in.Close() _ = out.Close() if nil == err { fmt.Println("copy file failed") } soFile := C.CString(path) defer C.free(unsafe.Pointer(soFile)) handle := C.load_so(soFile) if nil != handle { v := C.so_incr(handle, C.int(5)) C.release_so(handle) fmt.Println("return value from so:", int(v)) } }