Calling functions in such a file from Go

Is it possible to call a static object (.so) file from Go? I was a regular Google and I keep clicking on what I can do

lib, _ := syscall.LoadLibrary("...")

But trying this gives an error

undefined: syscall.LoadLibrary

and search through Godocs I cannot find a link to this function in the syscall package. Can I load a library and call its functions?

+4
source share
2 answers

On the POSIX platform, you can use cgoto call dlopen and friends:

// #cgo LDFLAGS: -ldl
// #include <dlfcn.h>
import "C"

import fmt

func foo() {
     handle := C.dlopen(C.CString("libfoo.so"), C.RTLD_LAZY)
     bar := C.dlsym(handle, C.CString("bar"))
     fmt.Printf("bar is at %p\n", bar)
}
+3
source

As @JimB said, you should just use CGO and put a link to the dynamic / static library. according to this example:

// #cgo LDFLAGS: -lpng
// #include <png.h>
import "C"

...

var x:= C.png_whatever() // whatever the API is

: http://blog.golang.org/c-go-cgo

+3

All Articles