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.”
