godepgraph

repository·master·Indexed 22 days ago

https://github.com/kisielk/godepgraph

A command-line tool used to generate visual dependency graphs of Go packages in Graphviz dot or Mermaid formats. It supports filtering by prefix or specific package, ignoring the standard library or vendored packages, and provides color-coded nodes to distinguish between standard library, cgo, and vendored packages.

Tokens
1.1K
Snippets
2
Records
9
Agent score
78%

What's inside godepgraph

  1. Understand godepgraph package color coding

    master

    The generated graphs use specific colors to distinguish between different types of Go packages:

    • green: Go standard library packages (found in $GOROOT).
    • blue: Regular Go packages (found in $GOPATH).
    • yellow: Vendored Go packages (found in $GOPATH).
    • orange: Packages in $GOPATH that use cgo (by importing the "C" package).
  2. Generate Go package dependency graphs

    master

    To generate a dependency graph, provide the package path as the first argument.

    • For a remote package: godepgraph <package_path>
    • For a local Go module project: use a relative path like godepgraph ./pkg/api.

    By default, the output is in Graphviz dot format. To render this as an image, pipe the output to the dot tool (requires Graphviz installed).

  3. Filter and ignore imports in godepgraph

    master

    Use the following flags to exclude specific types of packages from your dependency graph:

    • -s: Ignore the Go standard library entirely.
    • -novendor: Ignore vendored packages entirely.
    • -i <comma-separated-list>: Ignore specific import paths. Packages and their imports are excluded unless they are also required by a non-excluded package.
    • -p <comma-separated-list>: Ignore import paths by prefix.
  4. Generate dependency graphs in DOT or Mermaid formats

    master

    The godepgraph tool supports two output formats via the -format flag:

    1. dot: The default format, compatible with Graphviz.
    2. mermaid: A format compatible with Mermaid.js diagrams.

    If an invalid format is provided, the tool will exit with an error.

    Example usage for Mermaid:

    godepgraph -format mermaid ./cmd/myapp
  5. Generate Graphviz DOT output with graphvizPrinter

    master

    The graphvizPrinter type is used to generate dependency graphs in the Graphviz DOT language. It manages package IDs and applies specific colors to nodes based on their properties (e.g., Go standard library, Cgo usage, or vendored status).

    Key visual attributes applied to nodes:

    • Go Standard Library: palegreen (detected via Goroot)
    • Cgo usage: darkgoldenrod1 (detected via CgoFiles)
    • Vendored libraries: palegoldenrod (detected via isVendored)
    • Build errors: red (detected via hasBuildErrors)
    • Default: paleturquoise

    Each node includes a URL attribute pointing to the package documentation.

  6. Configure godepgraph CLI flags

    master

    The godepgraph CLI provides several flags to control how the dependency graph is generated, filtered, and formatted. You can use either the long-form flags or their short-form aliases.

    Filtering Packages

    • Ignore Standard Library: Use -nostdlib or -s to exclude packages in the Go standard library.
    • Include Standard Library Dependencies: Use -withgoroot or -d to show dependencies of packages in the Go standard library.
    • Ignore Vendor Directory: Use -novendor to exclude packages in the vendor directory.
    • Include Test Packages: Use -withtests or -t to include TestImports and XTestImports in the graph.
    • Ignore by Prefix: Use -ignoreprefixes or -p with a comma-separated list of prefixes to ignore.
    • Include Only Specific Prefixes: Use -onlyprefixes or -o with a comma-separated list of prefixes to include.
    • Ignore Specific Packages: Use -ignorepackages or -i with a comma-separated list of exact package names to ignore.

    Graph Construction and Output

    • Output Format: Use -format to choose between dot (Graphviz) or mermaid. The default is dot.
    • Layout Direction: Use -horizontal to lay out the graph horizontally instead of vertically.
    • Max Depth: Use -maxlevel or -l to set the maximum depth of the dependency graph (default is 256).
    • Build Tags: Use -tags with a comma-separated list of build tags to satisfy during the build process.

    Error Handling

    • Stop on Error: Use -stoponerror (defaults to true) to halt execution if a package import error occurs.