-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib_unix.go
63 lines (57 loc) · 1.41 KB
/
lib_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//go:build (freebsd || linux || darwin) && (amd64 || arm64)
package ffi
import (
"github.com/ebitengine/purego"
)
// Load loads a shared library at runtime.
//
// The name can be an absolute path, relative path or just the filename.
// If just the filename is passed, it will use the OS specific search paths.
//
// Example:
//
// var filename string
//
// switch runtime.GOOS {
// case "freebsd", "linux":
// filename = "libraylib.so"
// case "windows":
// filename = "raylib.dll"
// case "darwin":
// filename = "libraylib.dylib"
// }
//
// raylib, err := ffi.Load(filename)
func Load(name string) (l Lib, err error) {
l.Addr, err = purego.Dlopen(name, purego.RTLD_LAZY)
return
}
// Get retrieves the address of an exported function or variable.
//
// Example:
//
// // C code:
// // int magic_number = 42;
//
// magicNumber, err := lib.Get("magic_number")
// if err != nil {
// panic(err)
// }
//
// // prints 42
// fmt.Println(*(*int32)(unsafe.Pointer(magicNumber)))
//
// // if go vet yells "possible misuse of unsafe.Pointer",
// // you can do the following workaround:
// fmt.Println(**(**int32)(unsafe.Pointer(&magicNumber)))
func (l Lib) Get(name string) (addr uintptr, err error) {
return purego.Dlsym(l.Addr, name)
}
// Close deletes a reference to the library. If the reference count is zero,
// the library gets unloaded.
func (l Lib) Close() error {
if l.Addr == 0 {
return nil
}
return purego.Dlclose(l.Addr)
}