Documentation
ΒΆ
Overview ΒΆ
Package gotoon provides encoding for Token-Oriented Object Notation (TOON), a compact, human-readable format designed for passing structured data to Large Language Models with significantly reduced token usage.
TOON is optimized for uniform complex objects and provides 30-60% token reduction compared to JSON while maintaining high LLM comprehension accuracy.
Example usage:
data := map[string]interface{}{
"users": []map[string]interface{}{
{"id": 1, "name": "Alice", "role": "admin"},
{"id": 2, "name": "Bob", "role": "user"},
},
}
encoded, err := gotoon.Encode(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(encoded)
// Output:
// users[2]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
Index ΒΆ
Constants ΒΆ
const ( ListItemMarker = "-" ListItemPrefix = "- " )
List markers
const ( Comma = "," Colon = ":" Space = " " Pipe = "|" Tab = "\t" )
Structural characters
const ( OpenBracket = "[" CloseBracket = "]" OpenBrace = "{" CloseBrace = "}" )
Brackets and braces
const ( NullLiteral = "null" TrueLiteral = "true" FalseLiteral = "false" )
Literals
const ( Backslash = "\\" DoubleQuote = "\"" Newline = "\n" CarriageReturn = "\r" )
Escape characters
const ( DelimiterComma = "," DelimiterTab = "\t" DelimiterPipe = "|" )
Delimiters
const DefaultDelimiter = DelimiterComma
DefaultDelimiter is the default delimiter for arrays and tabular data
Variables ΒΆ
This section is empty.
Functions ΒΆ
func Encode ΒΆ
func Encode(input interface{}, opts ...EncodeOption) (string, error)
Encode converts any Go value to TOON format string.
The input value is normalized to a JSON-compatible representation:
- Primitives (bool, int, float, string) are encoded as-is
- Structs are converted to maps using exported fields (respects json tags)
- Slices and arrays remain as arrays
- Maps with string keys remain as objects
- time.Time is converted to RFC3339Nano format
- NaN and Infinity become null
- Nil, undefined, functions become null
Options can be provided to customize the encoding:
- WithIndent(n): Set indentation size (default: 2 spaces)
- WithDelimiter(d): Set delimiter for arrays ("," | "\t" | "|", default: ",")
- WithLengthMarker(): Add "#" prefix to array lengths (e.g., [#3])
Example with options:
encoded, err := gotoon.Encode(data,
gotoon.WithIndent(4),
gotoon.WithDelimiter("\t"),
gotoon.WithLengthMarker(),
)
Types ΒΆ
type EncodeOption ΒΆ
type EncodeOption func(*EncodeOptions)
EncodeOption is a function that modifies EncodeOptions
func WithDelimiter ΒΆ
func WithDelimiter(d string) EncodeOption
WithDelimiter sets the delimiter for array values and tabular rows
func WithIndent ΒΆ
func WithIndent(n int) EncodeOption
WithIndent sets the number of spaces per indentation level
func WithLengthMarker ΒΆ
func WithLengthMarker() EncodeOption
WithLengthMarker enables the length marker prefix for arrays
type EncodeOptions ΒΆ
type EncodeOptions struct {
// Indent is the number of spaces per indentation level (default: 2)
Indent int
// Delimiter is the delimiter to use for array values and tabular rows
// Valid values: "," (comma), "\t" (tab), "|" (pipe)
// Default: ","
Delimiter string
// LengthMarker when true adds "#" prefix to array lengths (e.g., [#3] instead of [3])
// Default: false
LengthMarker bool
}
EncodeOptions represents the options for encoding values to TOON format
type LineWriter ΒΆ
type LineWriter struct {
// contains filtered or unexported fields
}
LineWriter manages indented line output for TOON format
func NewLineWriter ΒΆ
func NewLineWriter(indentSize int) *LineWriter
NewLineWriter creates a new LineWriter with the specified indentation size
func (*LineWriter) Push ΒΆ
func (w *LineWriter) Push(depth int, content string)
Push adds a new line with the specified depth and content
func (*LineWriter) String ΒΆ
func (w *LineWriter) String() string
String returns the accumulated lines joined with newlines
