Go's standard library has no machine learning or AI packages, but it gives you everything around a model: concurrency, net/http, encoding/json, math/rand/v2, sort and slices. In practice you combine it with Gonum for numerics, ONNX Runtime or a model server for inference, and plain HTTP calls to hosted LLM APIs.
Go is rarely used to research or train models. It is a strong choice for serving them, building data pipelines, and wrapping AI features in reliable services.
The standard library contains no tensors, no automatic differentiation and no training loops. What it offers are the building blocks that production AI services need.
| Need | Standard library package |
|---|---|
| Call model APIs and model servers | net/http, encoding/json |
| Parallel preprocessing and batching | goroutines, sync, context |
| Reading datasets | encoding/csv, bufio, os |
| Random sampling and shuffling | math/rand/v2 |
| Sorting, searching, min and max | slices, sort, cmp |
| Basic math | math, math/big |
| Structured logs and metrics hooks | log/slog, expvar |
Everything else, such as matrices, statistics, classical models and neural networks, comes from third-party modules.
| Library | Use it for |
|---|---|
Gonum (gonum.org/v1/gonum) | Matrices, linear algebra, statistics, optimisation, plotting. The base for most numeric Go code. |
| GoLearn | Classical ML such as k-NN, decision trees and linear models with a scikit-learn style API. |
| Gorgonia | Graph-based computation and automatic differentiation for building small neural networks in Go. Its development pace is slow, so check recent activity before adopting it. |
| ONNX Runtime bindings | Running models trained in PyTorch or TensorFlow and exported to ONNX. |
| Hosted or self-hosted model servers | Calling LLMs and other models over HTTP or gRPC, for example OpenAI-compatible endpoints, Ollama or vLLM. |
The usual architecture is to train in Python, export the model, and let Go handle serving, validation, caching and orchestration. This keeps the strengths of each ecosystem and avoids rewriting training code in Go.
Gonum is the practical starting point for anything statistical. The example below fits a simple linear regression using only Gonum's stat package.
Install it with go get gonum.org/v1/gonum. For larger problems, use gonum.org/v1/gonum/mat for dense matrices, which calls optimised routines.
Feature extraction, tokenising and batching are often CPU-bound and independent per item, which suits Go's concurrency model. A bounded worker pool avoids creating a goroutine per record on huge datasets.
Each goroutine writes to a distinct slice index, so no mutex is needed. Closing the channel tells the workers to stop, and wg.Wait() guarantees all writes have finished before you read out.
Most Go AI code today talks to an inference service. Use net/http with a context deadline, because model calls can be slow and must never hang a request forever.
The same pattern works for LLM APIs: build the JSON request, set a timeout, check the status code, decode the response. Add retries with backoff for 429 and 5xx responses, and log each call with log/slog.
context.WithTimeout, and propagate the request context so cancelled requests stop work.sync.RWMutex or a dedicated cache library.Yes, with libraries such as Gorgonia, but the ecosystem is small compared with Python. Most teams train in PyTorch or TensorFlow and only run inference from Go.
Not in the standard library. GPU acceleration comes from external runtimes, such as ONNX Runtime or a separate model server, which Go calls through bindings or over the network.
It is a good fit for the service layer: APIs, agents that call tools, queues, streaming and data pipelines. Its fast start-up, static binaries and simple concurrency make deployment easy.
Use Gonum. Its stat, mat and distuv packages cover most statistical and linear algebra needs.
Go's standard library does not include machine learning, but it supplies the concurrency, HTTP, JSON and data handling that AI systems depend on. Use Gonum for numerics, ONNX Runtime or a model server for inference, and worker pools, timeouts and caching to keep AI features fast and predictable in production.