ffi

package
v0.1.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ffi loads native libraries, such as a Rust crate or a Zig package built as a dynamic library, and binds their functions to Go functions, without cgo.

It is experimental, as native modules are: its API and golet-plugin.json may change in breaking ways in any release.

A package names the libraries it binds in golet-plugin.json, next to its sources, with how to build them:

{"libraries": [{"name": "adder", "build": {"cargo": "./rust"}}]}

`golet dev` and `golet build` build them for the app's platform (cargo build or zig build) and put them among the app's resources; `golet native build` builds them for every platform, ready to publish, so that the apps of others download them instead of building them. The package embeds the manifest and binds the library's functions:

//go:embed golet-plugin.json
var manifest []byte

var adder = ffi.New("adder", manifest)

var fns struct {
	Add      func(a, b int32) int32
	Greeting func(name string) string `ffi:"adder_greeting"`
}

func Add(a, b int32) (int32, error) {
	if err := adder.Bind(&fns); err != nil {
		return 0, err
	}
	return fns.Add(a, b), nil
}

Bind binds the functions once; calling it again, from any goroutine, only returns the first call's result, cheaply, so functions call it before using fns. Its success orders the binding before what follows it.

Functions are called through purego (github.com/ebitengine/purego), which converts their arguments and results as the C ABI of the platform passes them: integers, floats, pointers, bools, strings (to and from NUL-terminated C strings) and, on some platforms, structs of those. Libraries are loaded once and never unloaded. ffi works on macOS, Linux and Windows on amd64 and arm64; elsewhere Load reports errors.ErrUnsupported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvVar

func EnvVar(name string) string

EnvVar returns the environment variable that overrides where a library is found: GOLET_NATIVE_ and its name in upper case, with characters other than letters and digits as underscores ("adder" is GOLET_NATIVE_ADDER).

func GoString

func GoString(p unsafe.Pointer) string

GoString returns a copy of the NUL-terminated C string p points to, "" for nil: the library may free p afterwards.

func SnakeCase

func SnakeCase(name string) string

SnakeCase returns the name of the function Bind binds to a field without a tag: AddNumbers is add_numbers, and ParseURL parse_url.

Types

type Library

type Library struct {
	// contains filtered or unexported fields
}

Library is a native library, loaded on first use. Its methods are safe for concurrent use.

func New

func New(name string, manifest []byte) *Library

New returns the library named name in manifest, the content of the package's golet-plugin.json. A nil manifest leaves the library to $GOLET_NATIVE_<NAME>, the app's resources, the executable's directory and the CLI's builds (see Path). New only reads the manifest: the library is loaded on first use, and an invalid manifest, or one that does not name the library, is reported then.

name is the library's name without its prefix and extension: "adder" is libadder.dylib on macOS, libadder.so on Linux and adder.dll on Windows, unless the manifest's files name others.

func (*Library) Bind

func (l *Library) Bind(fns any) error

Bind sets the function fields of the struct fns points to to call the library's functions, loading the library. A field's tag names its function, which is otherwise its name in snake case (AddNumbers calls add_numbers):

var fns struct {
	Add     func(a, b int32) int32
	Version func() string `ffi:"adder_version"`
	Extra   func()        `ffi:"adder_extra,optional"` // nil when missing
	Cache   map[string]int                               // not a function: left alone
	Hidden  func()        `ffi:"-"`                       // left alone
}

It binds them once: later calls with the same pointer, from any goroutine, return the first one's result without binding again, so functions can call Bind before each use of fns. Fields bound before the first call are left alone.

func (*Library) BindFunc

func (l *Library) BindFunc(fptr any, name string) error

BindFunc sets the function fptr points to to call the library's function named name, loading the library, once: later calls with the same pointer return the first one's result.

func (*Library) From

func (l *Library) From(dir string) *Library

From tells l the directory of the package that binds it, when the program knows it, as from runtime.Caller: the CLI's builds in that package's module are found too, whatever the working directory. It returns l, and is called before the library loads.

func (*Library) Load

func (l *Library) Load() error

Load loads the library, once: later calls return the first one's error.

func (*Library) Name

func (l *Library) Name() string

Name returns the library's name.

func (*Library) Path

func (l *Library) Path() (string, error)

Path returns the file Load loads, the first of:

  • the file $GOLET_NATIVE_<NAME> names (see EnvVar);
  • the one among the app's resources (golet.PathResources), where `golet build` and `golet dev` put it, signed with the app on macOS;
  • on macOS, the one in the app's Contents/Frameworks, where Swift libraries go;
  • the one next to the executable;

and, in programs that are not packaged apps, as under `go run` and `go test`:

  • the one the CLI built from sources (`golet native build -target host`, or `golet dev`), in .golet/native/<goos>-<goarch> of the module of the package's directory (see From) or of the working directory;
  • the published one in the user's cache (<cache>/golet/natives/<sha256>/), downloaded there when it is missing, checking its SHA-256.

Packaged apps never download libraries: build them with the CLI.

func (*Library) Symbol

func (l *Library) Symbol(name string) (uintptr, error)

Symbol returns the address of the symbol named name, loading the library.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL