liveBook · Manning

Global object

As explained in Learn Go with Pocket-Sized Projects

Global is a js package function that returns the global JavaScript object.

In Go’s JavaScript integration, js.Global() returns the global JavaScript object. Calling Set on it exposes a Go function everywhere on the page, allowing JavaScript handlers such as generate() to invoke Go code. section 12.2.2

Global exposure is convenient for a small application, but larger applications can avoid namespace pollution by placing functions on a dedicated JavaScript object. section 12.2.2

Registering a Go function globally

js.Global().Set("generate", js.FuncOf(m.generate)) JavaScript can then call generate(), for example from a button’s onclick handler. A namespaced alternative registers the function as multiplyApp.generate().

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains global registration, browser invocation, and namespace scoping, but does not detail the global object’s JavaScript internals.
Examples
Uses a multiplication exercise, a generate button, and the multiplyApp namespace to demonstrate global and scoped calls.
What it assumes
Assumes familiarity with Go functions, JavaScript calls, browser pages, and WebAssembly execution.
What you can do after
Apply it via js.Global and js.FuncOf to register callable Go functions and choose global or namespaced exposure.
How it connects
Connects JavaScript namespaces with WebAssembly, DOM access, event handlers, and js.FuncOf.

Also explained in

Compare how 2 books explain global object →

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.