Recursive AI

Getting started with oneui for Go

All oneui Go docs

Install oneui for Go, write a component, compile it with go generate, render it to HTML, test it without a browser, build a list with a two-way binding, read a template diagnostic, and run the component in a web page.

oneui for Go is a component model: a component is an HTML template, optional scoped CSS and a Go struct, and oneui generate compiles it into plain Go that updates only the nodes bound to state that changed. Every sample on this page was compiled and run with Go 1.26.8 against recursiveai.org/go/oneui v1.0.1 from the Go module proxy; the output under each one is what it printed.

Install

oneui needs Go 1.24 or newer.

Terminal
mkdir hello && cd hello
go mod init example.com/hello
go get recursiveai.org/go/oneui@v1.0.1

The module has no dependencies: its go.mod requires nothing. The license is MIT or Apache-2.0, at your choice. The generator runs through go run, so there is nothing else to install.

A component

A component is a template next to a Go struct. Hello.html:

HTML
<button on:click="Increment">Clicked {Count} times</button>

main.go:

GO
package main
 
//go:generate go run recursiveai.org/go/oneui/cmd/oneui generate
 
import (
	"os"
 
	"recursiveai.org/go/oneui"
)
 
type Hello struct {
	oneui.Base
	Count int `oneui:"state"`
}
 
func (h *Hello) Increment() { h.SetCount(h.Count + 1) }
 
func main() {
	h := &Hello{}
	h.Increment()
	oneui.RenderHTML(os.Stdout, h)
}
Terminal
go generate ./...
go run .
Text
<button>Clicked 1 times</button>

go generate checks every name in the template against the struct and writes three files: Hello_oneui.go (the SetCount setter and the component's shape), Hello_build_oneui.go (the code that creates the button once and rewrites only its text when Count changes) and reload_oneui.go (used only by builds with the oneui_reload tag, for template reload). Run it again after editing a template or upgrading oneui; a generated file built against another runtime version stops at start-up with a message that says so.

A field tagged oneui:"state" gets SetCount, which updates the bound nodes when the value changes, and NotifyCountChanged, for a value changed in place. oneui.RenderHTML writes the markup for the current state without creating nodes, for first paint or server-side rendering. Listeners and stylesheets are not in that markup, and mounting over it is not supported (no hydration).

Test it

oneuitest.Mount mounts a component in the reference document, an in-memory DOM with selector and accessibility queries. Click and Type act the way a user does, apply what the component did and fail the test on any error. hello_test.go:

GO
package main
 
import (
	"testing"
 
	"recursiveai.org/go/oneui/oneuitest"
)
 
func TestHello(t *testing.T) {
	page := oneuitest.Mount(t, &Hello{})
	page.Click("button")
	page.Click("button")
	if got := page.Text("button"); got != "Clicked 2 times" {
		t.Fatal(got)
	}
}
Terminal
go test -v .
Text
=== RUN   TestHello
--- PASS: TestHello (0.00s)
PASS
ok  	example.com/hello	0.002s

A list with a two-way binding

A component in its own package, todo/: an input bound to Draft with bind:value, an if/else block, and a keyed each block whose rows toggle a class on click. Todos.css is scoped to the component. todo/Todos.html:

HTML
<input bind:value="Draft" placeholder="New task">
<button on:click="Add">Add</button>
<template if="{len(Items) == 0}"><p>Nothing to do.</p></template>
<template else>
  <ul>
    <template each="{Items}" item="t" key="{t.ID}">
      <li class:done="{t.Done}" on:click="{Toggle(t.ID)}">{t.Title}</li>
    </template>
  </ul>
</template>

todo/Todos.css:

CSS
li.done { text-decoration: line-through; }

todo/todos.go:

GO
// Package todo is a task list: an input, a keyed list and a two-way binding.
package todo
 
import "recursiveai.org/go/oneui"
 
//go:generate go run recursiveai.org/go/oneui/cmd/oneui generate
 
type Item struct {
	ID    int
	Title string
	Done  bool
}
 
type Todos struct {
	oneui.Base
	Draft string `oneui:"state"`
	Items []Item `oneui:"state"`
}
 
func (c *Todos) Add() {
	if c.Draft == "" {
		return
	}
	c.SetItems(append(c.Items, Item{ID: len(c.Items) + 1, Title: c.Draft}))
	c.SetDraft("")
}
 
func (c *Todos) Toggle(id int) {
	for i := range c.Items {
		if c.Items[i].ID == id {
			c.Items[i].Done = !c.Items[i].Done
		}
	}
	c.NotifyItemsChanged()
}

Toggle changes an item in place, so it calls NotifyItemsChanged instead of the setter. todo/todos_test.go types into the input, adds the task and toggles it:

GO
package todo
 
import (
	"testing"
 
	"recursiveai.org/go/oneui/oneuitest"
)
 
func TestAddAndToggle(t *testing.T) {
	page := oneuitest.Mount(t, &Todos{})
	if got := page.Text("p"); got != "Nothing to do." {
		t.Fatal(got)
	}
	page.Type("input", "Write docs")
	page.Click("button")
	page.Click("li")
	if got := page.Text("li.done"); got != "Write docs" {
		t.Fatal(got)
	}
}
Terminal
go generate ./...
go test ./todo
Text
ok  	example.com/hello/todo	0.002s

Every generated component also has its own RenderHTML method. cmd/list/main.go renders the list with two items:

GO
package main
 
import (
	"log"
	"os"
 
	"example.com/hello/todo"
)
 
func main() {
	c := &todo.Todos{Items: []todo.Item{
		{ID: 1, Title: "Write docs", Done: true},
		{ID: 2, Title: "Tag v1.0.1"},
	}}
	if err := c.RenderHTML(os.Stdout); err != nil {
		log.Fatal(err)
	}
}
Text
<input data-oui-06ba41f3="" placeholder="New task" value=""><button data-oui-06ba41f3="">Add</button><ul data-oui-06ba41f3=""><li class="done" data-oui-06ba41f3="">Write docs</li><li data-oui-06ba41f3="">Tag v1.0.1</li></ul>

The data-oui-… attribute is what scopes Todos.css to this component. The bound Draft is written as the value attribute.

Template diagnostics

A name the struct doesn’t have stops go generate with its position in the template, and nothing is written. With {t.Title} changed to {t.Name} in Todos.html:

Text
Todos.html:7:62: OUI0031: Item has no field Name
exit status 1
todo/todos.go:6: running "go": exit status 1

The command reports every problem in one run and exits 1; go doc recursiveai.org/go/oneui/cmd/oneui lists its flags.

In the browser

Package recursiveai.org/go/oneui/browser, built with GOOS=js GOARCH=wasm, applies change sets to the page’s real DOM, routes DOM events back to your handlers and registers components as custom elements. web/main.go:

GO
//go:build js && wasm
 
package main
 
import (
	"log"
 
	"example.com/hello/todo"
	"recursiveai.org/go/oneui"
	"recursiveai.org/go/oneui/browser"
)
 
func main() {
	if err := browser.Define("x-todos", func() oneui.Component { return &todo.Todos{} }); err != nil {
		log.Fatal(err)
	}
	select {} // the page drives the program from here on
}

Build it and copy the loader from the same Go release:

Terminal
GOOS=js GOARCH=wasm go build -o web/app.wasm ./web
cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" web/

web/index.html:

HTML
<!doctype html>
<meta charset="utf-8">
<title>Todos</title>
<x-todos></x-todos>
<script src="wasm_exec.js"></script>
<script>
  const go = new Go();
  WebAssembly.instantiateStreaming(fetch("app.wasm"), go.importObject).then((r) => go.run(r.instance));
</script>

Serve web/ with any static file server. In Chrome, the page’s <x-todos> element holds:

HTML
<x-todos><input data-oui-06ba41f3="" placeholder="New task"><button data-oui-06ba41f3="">Add</button><p data-oui-06ba41f3="">Nothing to do.</p></x-todos>

The browser package installs its bundled script itself, which needs the page to allow Function construction. A page whose content security policy forbids that should load oneui-browser.js (browser.Glue() returns it) with a script tag.

Next

  • Overview: the README, with state, testing, HTML rendering and the browser backend.
  • Changelog: the 1.0.1 form submission fixes, every package in 1.0.0 and the names and shapes of the API.
  • API reference on pkg.go.dev, including ui and ui/kit, the retained UI tree and its widgets.