How do you create and use shared libraries in Go?

219 views
Skip to first unread message

st ov

unread,
Jun 26, 2017, 3:37:48 AM6/26/17
to golang-nuts
Ruby has Gems and .NET has DLLs.
How do you package and share libraries in Go?


Nathan Kerr

unread,
Jun 26, 2017, 4:04:54 AM6/26/17
to golang-nuts
Go packages are usually source code published in web-accessible repositories such as GitHub. The repositories for packages you want to use are checked out/cloned into GOPATH on your local machine using go get and the import path for that package.

Sharing a package you created is as easy as pushing the code for it to a web-accessible repository and making sure that imports of any packages in that repository use the full import path (e.g., github.com/username/pkgroot/pkg2).

Find packages at https://awesome-go.com and https://godoc.org (which also lets you see the documentation for any publicly-shared package).

Peter Mogensen

unread,
Jun 26, 2017, 10:47:14 AM6/26/17
to golan...@googlegroups.com


On 2017-06-26 09:37, st ov wrote:
> Ruby has Gems and .NET has DLLs.
> How do you package and share libraries in Go?

Gems are mainly source files, right? ... Using
source libraries in Go is relatively straight forward.
For your "package main" programs the easiest is to put the
library source in "vendor" - maybe using some vendoring tool. (of which
there are many)

Using .so files to dynamically link to compiled shared libraries are
somewhat in it's infancy in Go.

One of the problems for real use is:

https://github.com/golang/go/issues/19233

/Peter

st ov

unread,
Jun 26, 2017, 11:54:58 AM6/26/17
to golang-nuts, a...@b-one.net
I thought source could be shared and vendored directly as well.

But is the buildmode and linkedshared arguments to go install and go build another possibility?
What are they for?


Thanks!

st ov

unread,
Jun 27, 2017, 1:44:58 AM6/27/17
to golang-nuts, a...@b-one.net
Is it possible to share package through *.a files?


Also saw this running 'go help buildmode'

-buildmode=default

Listed main packages are built into executables and listed

non-main packages are built into .a files (the default

behavior).


-buildmode=shared

Combine all the listed non-main packages into a single shared

library that will be used when building with the -linkshared

option. Packages named main are ignored.

Igor Maznitsa

unread,
Jun 27, 2017, 5:18:00 AM6/27/17
to golang-nuts
Go also allows to produce plugins (DLL) which can be loaded dynamically, it also one of ways to share compiled libraries
and if I want share some library on sources level between several modules and get static linking then I just use multimodule project
Reply all
Reply to author
Forward
0 new messages