Go's standard library builds web-based user interfaces well, but it has no native desktop GUI toolkit. For server-rendered pages use net/http with html/template and serve assets with embed and http.FileServer. For richer front ends, serve a JSON API that a JavaScript framework consumes. For command-line interfaces use flag and bufio. Native GUIs need a third-party module.
The examples below use current practice: embedded templates, automatic HTML escaping, server timeouts and a JSON endpoint.
| Interface type | Standard library support | Typical approach |
|---|---|---|
| Server-rendered web pages | Yes: net/http, html/template | Templates compiled into the binary |
| Static sites and SPAs | Yes: embed, http.FileServer | Ship the built front end inside one binary |
| JSON APIs for React, Vue, Svelte | Yes: encoding/json | Go back end, JavaScript front end |
| Command-line tools | Yes: flag, os, bufio | Plain text or prompts |
| Native desktop GUI | No | Third-party toolkits such as Fyne or Wails |
| Compile to browser | Partial | GOOS=js GOARCH=wasm produces WebAssembly |
html/template escapes values according to context (HTML, attribute, JavaScript, URL), which protects against cross-site scripting. Parse templates once at startup, not on every request, and embed them so the binary has no external files.
Project layout:
templates/index.html:
main.go:
The second list item contains a script tag, and the template renders it as harmless escaped text. This is why you should use html/template and never text/template or fmt.Fprintf for HTML. The pattern GET /{$} (Go 1.22 and later) matches only the root path, so unknown URLs return 404 instead of the home page.
Use {{define "name"}} and {{template "name" .}} to share layouts, and {{block}} for overridable regions. Register helper functions with template.New("").Funcs(template.FuncMap{...}) before parsing.
Embed a built front end (for example the dist folder from Vite) and serve it from the same binary.
http.FileServerFS has been available since Go 1.22. Register it with mux.Handle("GET /", staticHandler()). Set cache headers for fingerprinted assets, and add a fallback to index.html if the single-page app uses client-side routing.
When the interface is built with React, Vue or Svelte, Go acts as the API.
Register it with mux.HandleFunc("GET /api/todos", listTodos). Decode request bodies with json.NewDecoder(r.Body) after wrapping the body in http.MaxBytesReader. If the front end runs on a different origin during development, add CORS headers deliberately for specific origins instead of using * with credentials. For pages that submit forms, protect state-changing requests against CSRF; Go 1.25 added http.CrossOriginProtection for this purpose.
The standard library does not include WebSocket support. For server push, Server-Sent Events work with plain net/http by setting Content-Type: text/event-stream and flushing through http.ResponseController:
Go can also compile to WebAssembly with GOOS=js GOARCH=wasm go build, using the syscall/js package to call browser APIs. The resulting binaries are large, so this suits tools and demos more than content sites.
For terminal tools, flag parses arguments and bufio.Scanner reads input:
For subcommands and rich terminal UIs, projects commonly add Cobra or Bubble Tea.
No. The standard library has no GUI toolkit. Fyne, Wails (which wraps a web view) and Gio are common third-party options.
It escapes by output context, which prevents most injection when you pass data as template values. Avoid converting untrusted strings to template.HTML, because that disables escaping.
Server-rendered pages with html/template are simpler and fast for content sites and admin tools. Choose a single-page app when the interface needs rich client-side interactivity.
Use the embed package with //go:embed and parse templates with template.ParseFS.
Use net/http and html/template for server-rendered interfaces, embed to package templates and static files, JSON endpoints for JavaScript front ends, and flag for command-line tools. Native GUIs and WebSockets require third-party modules.