js.FuncOf wraps a Go function as a js.Func that JavaScript can call. Register it with js.Global().Set or a DOM namespace, then keep the Go program running. section 12.2.2
A callback passed to js.FuncOf receives:
- this js.Value, representing the DOM element or JavaScript context that made the call. section 12.2.2
- args []js.Value, containing values supplied by JavaScript. section 12.2.2 section 12.2.3
- any, allowing the callback to return a value; returning an error causes the js package to panic. section 12.2.2
Expose and call a Go callback
Register generate with js.Global().Set("generate", js.FuncOf(m.generate)), then call generate() from HTML. A multiplication exercise uses the callback to generate operands and update DOM elements. Because the Go program must remain alive, main waits on an unclosed channel; otherwise JavaScript reports that the Go program has already exited.
Pass input arguments
An HTML form calls validate(providedAnswer.value). The Go callback checks len(args), converts args[0].String() with strconv.Atoi, reports invalid input through alert, and validates the multiplication answer.
