Go's standard library has no charting package, but it gives you everything needed to build data visualization and dashboards: net/http to serve pages, html/template to render them safely, encoding/json to feed data to the browser, image and image/png for raster output, and embed to ship assets inside the binary. The usual pattern is a Go backend that exposes JSON and a browser charting library, or Go generating SVG and PNG directly.
This article covers the three practical approaches and shows runnable code for each.
| Need | Standard library | When to add a module |
|---|---|---|
| Serve a dashboard | net/http, html/template, embed | Rarely |
| Send data to charts | encoding/json | Never |
| Draw shapes or PNG output | image, image/color, image/png | Text rendering needs golang.org/x/image/font |
| Generate SVG | text/template, fmt, strings.Builder | Optional |
| Real-time updates | net/http with Server-Sent Events | WebSockets need a module |
| High-level charts | None | gonum.org/v1/plot, go-echarts |
If you need axes, legends and many chart types without writing drawing code, use a plotting module such as Gonum plot, or send JSON to a browser library such as Chart.js or Apache ECharts.
Embed the template and static files into the binary with embed so deployment is one file. html/template escapes values for the context they appear in, which protects you from cross-site scripting.
The method and path patterns such as GET /api/sales need Go 1.22 or later. The file templates/index.html loads a charting library and fetches /api/sales:
In production, pin the library version and self-host it through embed instead of loading it from a CDN.
SVG is text, so a template or fmt is enough, and the output scales to any screen size. This example draws a bar chart.
Only numbers are interpolated into the markup. If you add labels from user data, pass them through html.EscapeString.
For emails, reports or places where JavaScript is not available, render pixels with image and image/png.
The standard library has no text drawing, so axis labels need golang.org/x/image/font. At that point a plotting module saves time.
For a live dashboard, a Server-Sent Events stream is simpler than WebSockets and needs no module. Browsers read it with EventSource.
This needs the fmt, math/rand/v2, net/http and time imports. The handler exits when the client disconnects because it watches the request context.
image package.No. Use image and SVG for simple output, or a module such as Gonum plot or go-echarts for full charts.
Yes. It applies context-aware escaping to HTML, JavaScript, CSS and URLs. Use text/template only for non-HTML output such as SVG you build yourself.
Stream events with Server-Sent Events, or use a WebSocket module when you need two-way messages.
Render in the browser when users interact with the chart. Render on the server when you need images for email, reports or crawlers.
Go's standard library serves data, renders templates, draws images and streams events, but it leaves high-level charts to modules or browser libraries. Pair a JSON API with a front-end chart for interactive dashboards, and use SVG or image for static output.