Can shared/dynamically linked libraries be written in Go?

Hi,

I understand it is possible to use existing shared libraries written in C/C++ etc in Go, but is it possible to write the shared libraries in Go and then use/include them dynamically in code?
The use case is for an application that require plugins and the application will use whatever plugins is specified.

Pieter

yes, starting with go-1.5, it’s possible. see:

@bins, thanks for the answer. So it is possible to link existing libraries written in other languages, but is it possible to write the library in Go, and then use that library dynamically in Go?

that’s buildmode=shared.
support varies among architectures and platforms. I believe go-1.6 has support for linux/amd64.

I haven’t worked out a recipe, though.
the tests are there:
https://github.com/golang/go/blob/master/misc/cgo/testshared/shared_test.go#L416

note that dlopen-ing a Go shared library from Go and using its symbols isn’t (AFAIK) working readily yet.
you’d have to use the C dlopen library and unsafe-cast to a Go value.
that’s what the plugin package is meant to do.
it’s not yet written. (but I am slowly working on it there: https://github.com/sbinet/go-plugin)

Thanks @bins, much appreciated

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.