Defining Golem Components in Zig
Creating a project
Golem's command line interface provides a set of predefined, Golem-specific examples to choose from as a starting point.
To see the available examples for Zig, run:
$ golem-cli list-examples --language zig
The set of examples provided by Golem CLI is defined in the open-source repository golem-examples (opens in a new tab).
Then to create a new project based on one the default Zig example, run:
$ golem-cli new --language zig --package-name 'golem:demo' example
The command will create a new Golem project in the example
directory, and print short, language-specific instructions on how to build the project.
Specification-first approach
Golem and the Zig (and C/C++) toolchain currently requires defining the component's interface using the WebAssembly Interface Type (WIT) format. See the official documentation of this format (opens in a new tab) for reference.
Each new project generated with golem-cli
contains a wit
directory with at least one .wit
file defining a world
. This world can contain exports (exported functions and interfaces) and these exports will be the compiled Golem component's public API.
The first time a component is compiled (see the Building Components page for details), a couple of files (example.c
, example.h
and example_component_type.o
if the component name was example
) get generated in the src/bindings
directory. This module contains the C definitions of all the data types and interfaces defined in the WIT
file(s).
To implement the specification written in WIT, the Zig code must implement some of these generated exported functions defined in the generated header file.
As currently there is no Zig-specific binding generator, for more details, check the C/C++ guide for defining components.