Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared library in Go?

Is it possible to create a Shared Library (.so) using Go?

UPDATED: created an "issue" for it.

like image 339
jldupont Avatar asked Nov 18 '09 16:11

jldupont


People also ask

Can you use C libraries in go?

Go allows linking against C libraries or pasting in in-line code.

Does Go have libraries?

There is plenty of packages or libraries for Go programming that supports developers in the production of application programs.

How do I make a package in go?

Check your GOPATH in environment variables and set it to the directory which contains all Go files. Create a new folder with the name of the package you wish to create. In the folder created in step 2, create your go file that holds the Go package code you wish to create.


2 Answers

This is possible now using -linkshared flag

What you need to do is to first run this command:

go install -buildmode=shared -linkshared  std 

(Above code makes all common packages shareable!) then

go install  -buildmode=shared -linkshared userownpackage 

finally when compiling your code you need to run:

go build -linkshared yourprogram 

What the above those is now it rather than statically linking everything only dynamically links them and you will end up with much smaller compiled files. Just to give you an idea my "hello.go" file with static linking is 2.3MB while the same code using dynamic linking is just 12KB!

like image 180
Reza Avatar answered Sep 20 '22 04:09

Reza


Go Execution Modes describes Building Go Packages as a shared library:

"In this mode a Go package, or set of packages, may be built as a shared library. A Go program that imports one or more of those Go packages may be linked against this shared library. The shared library may be changed between the time the Go program is linked and the time it is run; the shared library that is available when the program starts is the one that will be used...

In the Go 1.5 release this is implemented for the linux-amd64 target only. When using gccgo it is implemented for any supported target."

like image 34
mozey Avatar answered Sep 18 '22 04:09

mozey