ANN: dlopen/dlsym for Go - runtime dynamic library loading

857 views
Skip to first unread message

Alberto García Hierro

unread,
Jun 18, 2014, 7:52:39 AM6/18/14
to golan...@googlegroups.com
Hi,

I've just released an implementation of dlopen and dlsym for Go, which allow loading a dynamic libraries at runtime. For now, only linux/amd64 is working completely. There's a bug on darwin/amd64 which causes a crash when calling functions which require passing parameters on the stack, but everything else works fine.

The package uses cgo, reflection and a bit of asm code to generate Go functions at runtime. Here's a small example:

package dl_test


import (

        "bytes"

        "fmt"


        "gopkgs.com/dl.v1"

)


func ExampleOpen_snprintf() {

        lib, err := dl.Open("libc", 0)

        if err != nil {

                panic(err)

        }   

        var snprintf func([]byte, uint, string, ...interface{}) int 

        if err := lib.Sym("snprintf", &snprintf); err != nil {

                panic(err)

        }   

        buf := make([]byte, 200)

        snprintf(buf, uint(len(buf)), "hello %s!\n", "world")

        s := string(buf[:bytes.IndexByte(buf, 0)])

        fmt.Println(s)

        // Output: hello world!

}



Additionally, the package also supports loading symbols pointing to variables and automatically maps types between C and Go.

For install instructions and documentation, please see http://gopkgs.com/dl.v1

Regards,
Alberto
Reply all
Reply to author
Forward
0 new messages