Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic FFI in Go

Tags:

c

dynamic

go

ffi

Is it possible to dynamically load foreign C library (dll) and call its functions in Go?

I know there is cgo which is used to statically bind to C functions, but I'm interested in dynamic way.

like image 820
Marko Avatar asked Nov 09 '10 10:11

Marko


1 Answers

Short answer: no. (at least not with gc, gccgo is gcc linkable however, so it might be possible)

Medium answer: However, you can statically bind, say, libffi or libdl and then use it to dynamically load other libraries.

Long answer: You can write go packages in C and ASM using the go toolchains C compiler and assembler (see src/pkg/runtime for example). So you could write a FFI in either C or ASM as a go package.

Edit: From the comments below (also CW now)

Alternatively, it can be done using the syscall and unsafe packages (easily in windows, but I imagine it would be harder in linux and not far off from the third solution above).

http://code.google.com/p/go/wiki/CallingWindowsDLLs

like image 136
2 revs Avatar answered Oct 05 '22 06:10

2 revs