Hi,
I was looking at
purego dynamic loading library (without cgo) and loved the idea.
It seems to be working quite well across many OS/ARCH combinations.
You first Dlopen a library, then RegisterLibFunc functions from it.
In
sdl3 binding library, it is effectively used like this:
var sdlInit func(InitFlags) bool
func init() {
libSDL, _ := purego.Dlopen(libraryFileName, purego.RTLD_LAZY)
purego.RegisterLibFunc(&sdlInit, libSDL, "SDL_Init")
}
func Init(flags InitFlags) bool {
return sdlInit(flags)
}
So
- sdlInit is an unexported, mutable variable set at runtime (application start)
- Init is bound at link-time, exported and immutable (let's ignore function inlining)
- they have the exact same signature, you can do: sdlInit = Init
Go already allows implementing a function outside the language (in Assembly) like
func Init(flags InitFlags) bool
// implementation in Assembly
But this is still bound at link-time. What I am wondering is the possibility to have just the following and get rid of writable variables like sdlInit:
func Init(InitFlags) bool
func init() {
libSDL, _ := purego.Dlopen(libraryFileName, purego.RTLD_LAZY)
purego.RegisterLibFunc(&Init, libSDL, "SDL_Init")
}
So this is like filling a function pointer table at runtime (application startup). This would be doable only from inside init(). What do you think?
Regards..