Typorithm · hard

Go via c-shared & WASM

go-ffi-wasmarchitecturegowasmwebassembly
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Go via c-shared & WASM

0%

Native Integrations

Go can expose functions to Node by building with -buildmode=c-shared (a .so plus header) consumed through an FFI like ffi-napi. Alternatively, compile Go, Rust, or C to WebAssembly and run it via the WebAssembly API — sandboxed, cross-platform, no per-OS native build. WASM trades some speed and syscall access for portability: choose c-shared/addons for raw speed and full OS access, WASM for portability and sandboxing.

Go does not link into Node the way C++ or Rust addons do, but it can build a C-compatible shared library with go build -buildmode=c-shared, producing a .so and a header. Node then calls it through a foreign-function interface like ffi-napi, which dlopens the library and marshals arguments.

The other path is WebAssembly: compile Go (or Rust or C) to a .wasm module and run it with the WebAssembly API. WebAssembly.instantiate compiles the bytes and links them, and the exported functions hang off instance.exports — one portable artifact that runs on any OS with no native build per platform.

The trade-off is architectural. c-shared/native addons give raw speed and full operating-system access (files, sockets, syscalls) but must be compiled per platform and run with the host's privileges. WASM runs in a sandbox with limited syscall access and a small speed cost, but it is portable and isolated — pick by whether you value speed and OS access or portability and safety.

01

go build -buildmode=c-shared emits a .so + header for FFI.

02

WebAssembly.instantiate runs portable .wasm in Node.

03

c-shared = speed + OS access; WASM = portability + sandbox.

// Go: build a C-shared library
// go build -buildmode=c-shared -o lib.so main.go

// Or compile to WASM and run in Node
const wasm = await WebAssembly.instantiate(bytes);
const { add } = wasm.instance.exports;
console.log(add(2, 3));

Read the implementation

Connect each part of the code to the concept before you type.

1. c-shared build

Building Go with -buildmode=c-shared emits a .so plus a C header, callable through an FFI like ffi-napi.

// go build -buildmode=c-shared -o lib.so main.go

2. Load WASM

WebAssembly.instantiate compiles and links a .wasm module from its bytes — portable across OSes with no native build.

const wasm = await WebAssembly.instantiate(bytes);

3. WASM exports

Exported WASM functions appear on instance.exports, ready to call from JavaScript.

const { add } = wasm.instance.exports;

4. Call WASM

Calling the exported add runs inside the WASM sandbox and returns a number to JS.

console.log(add(2, 3));

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Go via c-shared & WASM: Lesson & Practice · Typorithm | ApplyDjinn