dot

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package dot writes graphs in the Graphviz DOT format (https://graphviz.org/doc/info/lang.html). Useful for quick visual inspection — pipe the output through 'dot -Tsvg' or 'dot -Tpng'.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write

func Write(w io.Writer, a *adjlist.AdjList[string, int64]) error

Write streams a DOT document representing a to w. The header uses 'digraph' for directed graphs and 'graph' for undirected. Edge weights are emitted as a label="..." attribute when non-zero.

Example

ExampleWrite renders a single directed, weighted edge as Graphviz DOT. The output is a digraph whose body lists "src -> dst" with the weight as the edge label; pipe it through `dot -Tsvg` to visualise.

package main

import (
	"bytes"
	"fmt"

	"github.com/FlavioCFOliveira/GoGraph/graph/adjlist"
	"github.com/FlavioCFOliveira/GoGraph/graph/io/dot"
)

func main() {
	g := adjlist.New[string, int64](adjlist.Config{Directed: true})
	_ = g.AddEdge("a", "b", 5)

	var buf bytes.Buffer
	if err := dot.Write(&buf, g); err != nil {
		panic(err)
	}
	fmt.Print(buf.String())
}
Output:
digraph G {
  a -> b [label="5"];
}
Example (MultiEdge)

ExampleWrite_multiEdge renders a small directed graph. The DOT writer emits one line per edge in an internal NodeID order, so the example sorts the body lines before printing to keep the output stable.

package main

import (
	"bytes"
	"fmt"
	"sort"
	"strings"

	"github.com/FlavioCFOliveira/GoGraph/graph/adjlist"
	"github.com/FlavioCFOliveira/GoGraph/graph/io/dot"
)

func main() {
	g := adjlist.New[string, int64](adjlist.Config{Directed: true})
	_ = g.AddEdge("a", "b", 1)
	_ = g.AddEdge("b", "c", 2)

	var buf bytes.Buffer
	if err := dot.Write(&buf, g); err != nil {
		panic(err)
	}

	// Collect just the indented edge lines and sort them for a
	// deterministic, layout-independent rendering.
	var edges []string
	for _, line := range strings.Split(buf.String(), "\n") {
		if strings.HasPrefix(line, "  ") {
			edges = append(edges, strings.TrimSpace(line))
		}
	}
	sort.Strings(edges)
	for _, e := range edges {
		fmt.Println(e)
	}
}
Output:
a -> b [label="1"];
b -> c [label="2"];

func WriteCtx

func WriteCtx(ctx context.Context, w io.Writer, a *adjlist.AdjList[string, int64]) error

WriteCtx is the context-aware variant of Write. ctx.Err() is checked once per source vertex; on cancellation flushes the buffer and returns the wrapped ctx.Err.

Types

This section is empty.

Jump to

Keyboard shortcuts

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