liveBook · Manning

Js.Value

As explained in Learn Go with Pocket-Sized Projects

js.Value is the syscall/js representation of a JavaScript value accessible from Go.

js.Value represents a JavaScript value that Go can access through syscall/js. In a JavaScript callback, values arrive as []js.Value; the first argument can be converted with String() and parsed in Go. Go can also reach page elements through js.Global().Get("document"), then read or update properties with Get, Call, and Set. section 12.2.3

Reading and validating an input

A validate function can reject an empty argument, read args[0].String(), parse it with strconv.Atoi, and use js.Global().Call("alert", ...) for feedback. It can retrieve the document element and reset its value with document.Call("getElementById", "providedAnswer").Set("value", ""). section 12.2.3

Listing 12.15 main.go: Retrieving operands
func (m *multiplication) validate(this js.Value, args []js.Value) any {
     if len(args) == 0 {
         return nil
     }

    document := js.Global().Get("document")

    guess := args[0].String()
    numGuess, err := strconv.Atoi(guess)
    if err != nil {
        // Handle error 
        return nil
    }
    ...
}

Shows the complete operand-retrieval path from a callback argument to a parsed Go number. section 12.2.3

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains callback arguments and DOM operations, including String, Get, Call, and Set, without expanding beyond the input-validation task.
Examples
Demonstrates validating a multiplication answer, displaying alerts, retrieving an input value, and clearing the field.
What it assumes
Assumes Go code can register JavaScript callbacks and interact with an HTML page.
What you can do after
Apply it via syscall/js to read callback arguments, access DOM values, call JavaScript functions, and update page elements.
How it connects
Connects JavaScript functions, DOM elements, HTML forms, callbacks, and numeric parsing.

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.