I’m considering Go for a new project that is supposed to be usted by other languages. So, the solution seems to be to create a shared library (Linux only).
I know this can be done with cgo, but I would like to see a real project that uses the same model.
Does anyone knows about a project that is used as a shared library?
评论:
kostix:
igknighted:the solution seems to be to create a shared library (Linux only). I know this can be done with cgo
This is not correct: the Go compiler supports a special "build mode" called "c-shared" which produces a shared library with C-style API. The
cgo
subsystem is not involved in this process (in the sense you don't useimport "C"
and stuff); you just supply the-buildmode=c-shared
command-line option to the compiler — see this and this.
tv64738:Working example test I did for something I was trying out recently. I did a c-archive instead of a shared library, but the principal is similar.
https://gist.github.com/protosam/edcc00cc309b5f4a163ece39c46d9e4a
That'll still need the Go runtime active, it won't behave like a C library. I would recommend against trying to embed Go in another language.
