CLI Reference#

Usage#

gowasm-bindgen <source.go> [options]

By default, gowasm-bindgen generates bindings, copies the runtime, and compiles WASM in one step.

Flags#

FlagDefaultDescription
-o, --output DIRgeneratedOutput directory for all artifacts
--no-buildfalseSkip WASM compilation (generate only)
--compiler NAMEtinygoCompiler: tinygo or go
-m, --mode MODEworkerGeneration mode: sync or worker
-c, --class-name NAMEGo<DirName>TypeScript class name
--optimizetrueEnable size optimizations (tinygo only)
-v, --verbosefalseEnable debug output to stderr

Examples#

Full Build (Default)#

Generates bindings, copies runtime, and compiles WASM:

gowasm-bindgen wasm/main.go

Creates:

  • generated/go-wasm.ts - TypeScript client (kebab-case from class name)
  • generated/worker.js - Web Worker entry point
  • generated/wasm_exec.js - Go runtime (copied from TinyGo)
  • generated/wasm.wasm - Compiled WASM binary
  • wasm/bindings_gen.go - Go WASM wrapper functions

Generate Only#

Skip WASM compilation (useful for CI or custom build pipelines):

gowasm-bindgen wasm/main.go --no-build

Custom Output Directory#

gowasm-bindgen wasm/main.go --output build/

Standard Go Compiler#

For larger binary with full Go compatibility:

gowasm-bindgen wasm/main.go --compiler go

Sync Mode#

Generates synchronous API that runs on main thread (blocks UI):

gowasm-bindgen wasm/main.go --mode sync

Creates:

  • generated/go-wasm.ts - TypeScript client with synchronous methods
  • wasm/bindings_gen.go - Go WASM wrapper functions

No worker.js is generated in sync mode.

Custom Class Name#

The default class name is derived from the directory: Go + TitleCase(dirname).

DirectoryDefault Class NameTypeScript File
wasm/GoWasmgo-wasm.ts
image-wasm/GoImageWasmgo-image-wasm.ts
go/Gogo.ts

Override with --class-name:

gowasm-bindgen wasm/main.go --class-name ImageProcessor
# Creates: generated/image-processor.ts with class ImageProcessor

Debug Output#

Troubleshoot generation issues:

gowasm-bindgen main.go --verbose

Output Files#

TypeScript Client#

The generated TypeScript client exports a class:

// Worker mode (from wasm/ directory)
export class GoWasm {
  static async init(workerUrl: string): Promise<GoWasm>;
  greet(name: string): Promise<string>;
  terminate(): void;
}

// Sync mode
export class GoWasm {
  static async init(wasmSource: string | BufferSource): Promise<GoWasm>;
  greet(name: string): string;  // No Promise
}

worker.js#

Web Worker script that loads and runs WASM (worker mode only).

bindings_gen.go#

Go WASM wrapper functions with //go:build js && wasm tag:

//go:build js && wasm
package main

import "syscall/js"

func init() {
    js.Global().Set("greet", js.FuncOf(wasmGreet))
    // ...
}

wasm_exec.js#

Go runtime copied from your TinyGo or Go installation.

Build Workflow#

With the default settings, a single command does everything:

gowasm-bindgen wasm/main.go

For more control, use --no-build and run compilation separately:

# Generate bindings only
gowasm-bindgen wasm/main.go --no-build

# Compile with custom flags
tinygo build -o generated/wasm.wasm -target wasm -opt=2 ./wasm/