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

graph

package
v0.2.45 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package graph implements the directed-graph primitives that architecture and other dependency-aware packages use.

It exposes a generic Graph[T] with topological sort and cycle detection — the building blocks for resolving service and module dependencies into a stable execution order.

Index

Constants

View Source
const (
	KindRepo     = "repo"
	KindModule   = "module"
	KindService  = "service"
	KindFile     = "file"
	KindEndpoint = "endpoint"
)

NodeKind is the type of a node in the graph (cross-repo, cross-language).

View Source
const (
	EdgeDependsOn = "depends_on"
	EdgeContains  = "contains"
	EdgeChildOf   = "child_of"
)

EdgeKind is the type of an edge.

Variables

This section is empty.

Functions

func FileID

func FileID(scopeRepo, scopeModule, scopeService, file string) string

FileID returns a node ID for a file within a scope.

func NodeID

func NodeID(parts ...string) string

NodeID builds a stable cross-repo node ID from components.

Types

type Edge

type Edge struct {
	From string
	To   string
	Kind string
}

Edge is a directed edge with an optional kind for cross-repo/cross-language graphs.

type Graph

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

Graph is a generic directed graph of nodes and edges for operational dependency trees.

func New

func New(name string) *Graph

New creates a new empty graph.

func (*Graph) AddEdge

func (g *Graph) AddEdge(from, to, kind string)

AddEdge adds a directed edge. Creates nodes if they do not exist (with empty kind).

func (*Graph) AddNode

func (g *Graph) AddNode(id, kind string) *Node

AddNode adds or updates a node. Returns the node for chaining.

func (*Graph) Children

func (g *Graph) Children(id string) []string

Children returns node IDs that are targets of edges from id.

func (*Graph) Edges

func (g *Graph) Edges() []Edge

Edges returns all edges.

func (*Graph) HasNode

func (g *Graph) HasNode(id string) bool

HasNode returns true if the node exists.

func (*Graph) InEdges

func (g *Graph) InEdges(to string) []Edge

InEdges returns edges into the given node.

func (*Graph) Merge

func (g *Graph) Merge(other *Graph)

Merge merges another graph into g. Node IDs must be unique; same ID overwrites (kind/attrs).

func (*Graph) Name

func (g *Graph) Name() string

Name returns the graph name.

func (*Graph) Node

func (g *Graph) Node(id string) *Node

Node returns the node by ID or nil.

func (*Graph) Nodes

func (g *Graph) Nodes() []*Node

Nodes returns all nodes (order not specified).

func (*Graph) OutEdges

func (g *Graph) OutEdges(from string) []Edge

OutEdges returns edges from the given node.

func (*Graph) Parents

func (g *Graph) Parents(id string) []string

Parents returns node IDs that have edges into id.

func (*Graph) Print

func (g *Graph) Print(verb string) string

Print returns a human-readable list of edges.

func (*Graph) ReachableFrom

func (g *Graph) ReachableFrom(fromID, toID string) bool

ReachableFrom returns true if toID is reachable from fromID.

func (*Graph) SetAttr

func (g *Graph) SetAttr(id, kind, key string, value any)

SetAttr sets an attribute on a node (creates node if missing).

func (*Graph) SubgraphFrom

func (g *Graph) SubgraphFrom(startID string) (*Graph, error)

SubgraphFrom returns a new graph containing all nodes reachable from startID (following edges).

func (*Graph) TopologicalSort

func (g *Graph) TopologicalSort() ([]string, error)

TopologicalSort returns node IDs in topological order (no cycle check; use on DAGs).

type Node

type Node struct {
	ID    string
	Kind  string
	Attrs map[string]any
}

Node is a vertex in the graph with a stable ID, kind, and optional attributes. IDs should be unique and stable across repos/languages (e.g. "repo/module/service", "repo/module/service/file.go").

Jump to

Keyboard shortcuts

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