/godynamic

load and unload dynamic library

Primary LanguageGoApache License 2.0Apache-2.0

GoDynamic

Build Status

GoDynamic can load and run Golang dynamic library compiled by -buildmode=shared -linkshared

How does it work?

GoDynamic works like a dynamic linker: use dl loads an .so libraray and lookup symbol, can unload.

Please note that GoDynamic is not a scripting engine. All features of Go are supported, and run just as fast and lightweight as native Go code.

Comparison with plugin

GoDynamic reuses runtime by libstd.so, which makes it much smaller. And code loaded by GoDynamic is unloadable.

Build

Make sure you're using go >= 1.10.

Examples

export GO111MODULE=auto
go install -buildmode=shared std
go build -linkshared -v github.com/pkujhd/godynamic/examples/loader

go install -v -buildmode=shared -linkshared github.com/pkujhd/godynamic/examples/ubase
./loader -l $GOPATH/pkg/linux_`go env GOARCH`_dynlink/libgithub.com-pkujhd-godynamic-examples-ubase.so -p github.com/pkujhd/godynamic/examples/ubase.so -r github.com/pkujhd/godynamic/examples/ubase.Enter -times 10

go install -v -buildmode=shared -linkshared github.com/pkujhd/godynamic/examples/uschedule
./loader -l $GOPATH/pkg/linux_`go env GOARCH`_dynlink/libgithub.com-pkujhd-godynamic-examples-uschedule.so -p github.com/pkujhd/godynamic/examples/uschedule.so -r github.com/pkujhd/godynamic/examples/uschedule.Enter -times 10

go install -v -buildmode=shared -linkshared github.com/pkujhd/godynamic/examples/uhttp
./loader -l $GOPATH/pkg/linux_`go env GOARCH`_dynlink/libgithub.com-pkujhd-godynamic-examples-uhttp.so -p github.com/pkujhd/godynamic/examples/uhttp.so -r github.com/pkujhd/godynamic/examples/uhttp.Enter

This has currently only been tested and developed on: Golang 1.10-1.19 (x64/x86, linux)