liveBook · Manning

Syscall/js

As explained in Learn Go with Pocket-Sized Projects

The js package is the Go WebAssembly interoperability package that represents JavaScript values and exposes calls between Go and JavaScript.

syscall/js is Go’s WebAssembly interoperability package for calling JavaScript, reading and updating the browser DOM, and exposing Go functions to JavaScript. Its API is experimental, so browser and Go changes may require adjustments. section 12.2.1

The package supports a small browser application by enabling Go to:

  • Call JavaScript functions and update properties with Call and Set, such as writing innerHTML. section 12.2.1
  • Register callbacks through js.FuncOf and make them visible globally or under a JavaScript namespace. section 12.2.2
  • Read JavaScript arguments as js.Value values, convert input strings, and report invalid answers with alert. section 12.2.3

Interactive multiplication exercise

A Go WebAssembly program generates random operands, writes them into HTML spans, registers generate and validate callbacks, reads the submitted answer, and alerts the user when the answer is invalid or incorrect. The Go program must remain alive after registration; reading from an empty channel prevents callbacks from failing with “Go program has already exited.”

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains DOM calls, callbacks, argument conversion, lifecycle, and user feedback, but defers broader API details.
Examples
Uses a browser multiplication exercise with generated operands, callbacks, answer parsing, alerts, and input-field resetting.
What it assumes
Assumes Go, HTML, JavaScript, and WebAssembly basics while demonstrating browser interoperation from working code.
What you can do after
Build interactive Go WebAssembly pages that update the DOM and respond to JavaScript-triggered events.
How it connects
Ties syscall/js to WebAssembly, JavaScript, the DOM, callbacks, and Go functions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.