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

ndjson

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ndjson writes the connector's stdout protocol: one JSON value per line, never torn.

Intake's pointer lines and admission's verdict lines are both read by a process parsing the stream and watched by a person, so both need the same two guarantees. A line is written whole or reported failed, and concurrent writers never interleave inside a line. Callers sanitize API-controlled strings before encoding (richtext.SanitizeTerminal): JSON escapes C0 controls but passes C1 controls such as U+009B through as raw UTF-8.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

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

Writer serializes whole lines onto w. The zero value with a nil w discards.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns the Writer for w. A nil w discards every line.

There is one Writer, and so one lock, per sink for the life of the process. Intake and admission each build their writer from the stdout they are handed; if those were two Writers their locks would not coordinate, and a pointer line and a verdict line could tear each other inside one write. Keying on the sink makes that impossible whoever calls this, without every caller having to agree to pass one instance around.

A value-typed sink is the one case this cannot settle by itself: two copies can wrap one underlying writer and compare unequal, and hashing one can panic, so each gets its own Writer. A composition that puts two producers on one such sink passes them one Writer instead — intake's Options.Lines and admission's RunOptions.LineWriter are there for exactly that.

Only reference-like sinks are keyed — a pointer, a channel, an unsafe pointer — which covers every real one, os.Stdout included. A value-typed sink is a copy rather than the same sink, and hashing one can panic when it holds an uncomparable field, so it gets a Writer of its own. The map holds one entry per distinct sink for the life of the process; a connector has one stdout.

func (*Writer) WriteLine

func (l *Writer) WriteLine(v any) error

WriteLine encodes v and writes it followed by a newline, whole.

A writer that takes part of the line is written the rest, and one that takes none without an error has failed (io.ErrShortWrite): a torn line is worse than no line to whoever parses the stream.

Jump to

Keyboard shortcuts

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