# ondrajz/go-callvis architecture

> Architecture diagram of the GitHub repository ondrajz/go-callvis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ondrajz/go-callvis
- Repository: https://github.com/ondrajz/go-callvis

## Overview

##### Project Type and Purpose
- This is a development tool aimed at visualizing the call graph of Go programs. Its main purpose is to run pointer analysis on a target package, generate a call graph in DOT language, and then render it either in an interactive web view (with an embedded HTTP server) or as a static image.
- The README highlights features like grouping functions, filtering by package, and integration with Graphviz, which indicates a focus on visual clarity and usability for developers exploring code dependencies.

##### Analyzing the File Structure for Architectural Insights
- Primary Source Files: The root-level Go files (analysis.go, dot.go, dot_cgo.go, dot_nocgo.go, handler.go, main.go, output.go, version.go) suggest a modular design where different files handle distinct parts of the processing – from analysis, DOT conversion to output.
- Examples Directory: Contains a sample project that demonstrates how the tool works, which reinforces its instructional and utility value.
- Configuration and CI Files: Files under .github, Makefile, and _config.yml reflect standard project management, build, and deployment practices rather than runtime architecture, yet they ensure quality and consistency.
- Assets: The images directory obviously contains supporting visuals (e.g., the gopher logo and screenshots) used in documentation.

##### Main Components
a. Input & CLI Parsing Module
- The main entry point (main.go) processes command-line arguments. This includes options to run in server mode, specify file outputs, set grouping preferences (e.g., by package or type), or use Graphviz for rendering.
b. Pointer Analysis Engine
- The analysis.go file (and related files) encapsulate the core logic that leverages Go’s pointer analysis techniques (via the golang.org/x/tools/go/pointer package) to generate the call graph data.
c. DOT File Generation Module
- The dot.go, dot_cgo.go, and dot_nocgo.go files form the layer that converts the analyzed data into the DOT language. This is key because it bridges the analysis output with the visualization step.
d. Output & Rendering Module
- The output.go and handler.go files manage how the DOT output is either served through an interactive HTTP server (default: localhost:7878) for dynamic SVG visualization or rendered into a static file (with support for various output formats such as svg, png, or jpg). This module might also handle the integration with Graphviz when the -graphviz flag is used.
e. Versioning and Metadata
- The version.go file ensures that version information is easily accessible, which is important for a tool that integrates into development workflows.

##### Relationships and Interactions
- The CLI module (main.go) initiates the process by parsing input options and determining whether to run in interactive server mode or to perform a one-off static rendering.
- The pointer analysis engine processes the target Go package to determine its call graph. Once complete, the results are passed to the DOT generator.
- The DOT generator module converts this data into a DOT format that outlines nodes (representing functions/methods and packages/types) and edges (representing different call types, with styling based on exported status, anonymity, etc.).
- The output module then takes over:
- If in server mode, it starts an HTTP server (as defined in handler.go) that allows users to interactively navigate the generated call graph.
- Alternatively, if the static file mode is selected, it invokes Graphviz (if specified) or performs local conversion to output the final image.

##### Architectural Patterns or Design Principles
- Modularity: Components are separated across analysis, conversion, and rendering stages; each has a distinct responsibility.
- Extensibility: The use of flags like -group, -ignore, -include, etc., suggest the architecture is designed to allow further enhancements and customizations.
- Separation of Concerns: CLI parsing, core logic (analysis), DOT generation, and output handling are independently maintained.
- Integration: The design includes external tool integration (Graphviz) and leverages Go’s built-in HTTP server to provide interactive capabilities.

- Components and labels for:
- CLI/Entry Point (main.go)
- Pointer Analysis Module (analysis.go and related files)
- DOT Generation Module (dot*.go files)
- Output & Rendering Module (handler.go, output.go)
- HTTP Server for Interactive Viewing
- External Integration with Graphviz
- From user input/arguments → to analysis, then to DOT generation, then splitting to either static file generation or interactive web serving.
- Core processing modules versus external services.
- Different types of outputs (static file versus interactive mode).
- Annotations for key architectural decisions (e.g., modular design, separation of concerns, extensibility through flag-based configurations).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    U["User Input/CLI Args"]:::input
    CLI["CLI/Entry Point"]:::cli
    PA["Pointer Analysis Engine"]:::analysis

    %% Subgraph for DOT File Generation Module
    subgraph "DOT File Generation Module"
        D1["DOT (dot.go)"]:::dot
        D2["DOT CGO (dot_cgo.go)"]:::dot
        D3["DOT NoCGO (dot_nocgo.go)"]:::dot
        DOC["DOT Combined Output"]:::dot
    end

    %% Subgraph for Output & Rendering Module
    subgraph "Output & Rendering Module"
        SO["Static Rendering (output.go)"]:::output
        HS["HTTP Server (handler.go)"]:::output
    end

    G["Graphviz Integration"]:::external
    V["Versioning & Metadata (version.go)"]:::version

    %% Connections
    U --> CLI
    CLI --> PA

    %% Pointer Analysis directs to each DOT file generation component
    PA --> D1
    PA --> D2
    PA --> D3

    %% Each DOT file node converges into a combined DOT output node
    D1 --> DOC
    D2 --> DOC
    D3 --> DOC

    %% Combined DOT output is used by both output modules
    DOC --> SO
    DOC --> HS

    %% Static rendering integrates with external Graphviz
    SO --> G

    %% Also link Versioning separately (for reference)
    CLI --- V

    %% Click Events
    click CLI "https://github.com/ondrajz/go-callvis/blob/master/main.go"
    click PA "https://github.com/ondrajz/go-callvis/blob/master/analysis.go"
    click D1 "https://github.com/ondrajz/go-callvis/blob/master/dot.go"
    click D2 "https://github.com/ondrajz/go-callvis/blob/master/dot_cgo.go"
    click D3 "https://github.com/ondrajz/go-callvis/blob/master/dot_nocgo.go"
    click SO "https://github.com/ondrajz/go-callvis/blob/master/output.go"
    click HS "https://github.com/ondrajz/go-callvis/blob/master/handler.go"
    click V "https://github.com/ondrajz/go-callvis/blob/master/version.go"

    %% Styles
    classDef input fill:#dfefff,stroke:#31708f,stroke-width:2px;
    classDef cli fill:#E8F6F3,stroke:#1B9CFC,stroke-width:2px;
    classDef analysis fill:#FFEFDB,stroke:#F08A5D,stroke-width:2px;
    classDef dot fill:#DFFF00,stroke:#9BC53D,stroke-width:2px;
    classDef output fill:#FDEDEC,stroke:#ED4C67,stroke-width:2px;
    classDef external fill:#E5E5E5,stroke:#4A4A4A,stroke-width:2px;
    classDef version fill:#fff3cd,stroke:#856404,stroke-width:2px;
```
