Help us shape the future of Go! Take the 2026 Go Developer Survey

runtime

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package runtime is linked into every compiled notebook. The kernel starts the notebook's binary as a worker and drives it through Serve: it asks the worker to run a cell, to hand over the value of a notebook variable, or to take one back after a rebuild, so that cells whose code and inputs did not change are not run again.

The protocol is one JSON object per line on the worker's standard input and output. What a cell prints goes to a pipe of its own, so it can never be mistaken for a reply.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bundle

func Bundle(v any) map[string][]byte

Bundle returns the MIME bundle of a value: its own when it implements Displayer, else its fmt %v text. A Displayer without text/plain (a figure that is only SVG and PNG) gets its type, <*plot.Figure>, rather than the %v of its innards.

func Decode

func Decode(ptr any, data []byte) error

Decode is the inverse of Encode.

func Encode

func Encode(ptr any) ([]byte, error)

Encode encodes the value ptr points to: nil as nil, a value with its own MarshalBinary with it, anything else with gob when every field it can hold is exported (gob would silently drop the others). Anything else is an error, which makes the value volatile.

func Serve

func Serve(p Program)

Serve answers the kernel until it says exit or closes the input.

Types

type Cell

type Cell struct {
	// contains filtered or unexported fields
}

Cell is handed to each compiled cell; the generated code calls Show with the cell's final expression.

func (*Cell) Result

func (c *Cell) Result(v any, err error)

Result shows v, the value of a call that also returned an error, or, when err is not nil, fails the cell with it, so the cells that read this one do not run on a value that is not there.

func (*Cell) Show

func (c *Cell) Show(vs ...any)

Show records the display of each value. A nil interface — the nil error of a call run for its effect — shows nothing.

type Displayer

type Displayer interface {
	Display() map[string][]byte
}

Displayer is implemented by values that know how to show themselves: the result maps a MIME type ("text/plain", "text/html", "image/svg+xml", "image/png", ...) to the bytes of that representation.

type Program

type Program struct {
	// Cells holds the compiled cells by their index among the notebook's
	// code cells; a cell that defines nothing and shows nothing still has
	// an entry.
	Cells []func(*Cell)

	// Vars maps each notebook variable to a pointer to it.
	Vars map[string]any
}

Program is what the generated main hands to Serve.

type Reply

type Reply struct {
	Cell    int                 `json:"cell,omitempty"`
	Name    string              `json:"name,omitempty"`
	Output  string              `json:"output,omitempty"`  // what the cell printed
	Display []map[string][]byte `json:"display,omitempty"` // what it showed
	Error   string              `json:"error,omitempty"`   // a panic, with its stack
	Took    time.Duration       `json:"took,omitempty"`    // how long the cell ran, in the worker
	Data    []byte              `json:"data,omitempty"`    // an encoded value
	// Volatile reports a value that cannot be encoded (a func, a channel,
	// unexported state): the cell that makes it has to run again instead.
	Volatile bool `json:"volatile,omitempty"`
}

Reply answers a Request.

type Request

type Request struct {
	Op   string `json:"op"` // "run", "get", "set" or "exit"
	Cell int    `json:"cell,omitempty"`
	Name string `json:"name,omitempty"`
	Data []byte `json:"data,omitempty"`
}

Request is a command from the kernel.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL