Go's standard library does not include machine learning algorithms, tensors or neural network layers. What it does give you is the infrastructure around ML: parsing datasets, basic math, concurrent preprocessing, and HTTP services that serve models. For training or heavy numerics, Go programs typically use Gonum or call out to a model server or runtime.
This article shows what the standard library can do for ML and AI work in Go, with runnable examples, and where you should reach for something else.
| Need | Standard library | Beyond it |
|---|---|---|
| Load CSV or JSON datasets | encoding/csv, encoding/json | Data frame libraries |
| Basic math and statistics | math, math/rand/v2, slices | Gonum (matrices, statistics, optimisation) |
| Parallel preprocessing | goroutines, sync, channels | None needed |
| Serve predictions over HTTP | net/http, encoding/json | gRPC, model servers |
| Neural networks, GPUs | Nothing | Bindings to ONNX Runtime or TensorFlow, or a remote inference API |
A common production pattern is to train in Python, export the model, and use Go for the service that loads data, calls inference and serves requests.
encoding/csv reads a dataset, and strconv converts text columns to numbers. Check every conversion, since real data has blanks and typos.
Avoid io/ioutil here: it has been deprecated since Go 1.16 in favour of io and os.
Simple models are short enough to write directly. Ordinary least squares linear regression needs only means and sums.
The original version of this example divided by zero when all x values matched and ignored mismatched lengths by returning zeros. Returning an error is the idiomatic fix. The same approach works for k-nearest neighbours or k-means, but for anything with matrices use Gonum.
Preprocessing is often embarrassingly parallel. This version computes a feature for every row on several workers and writes each result into its own slot, so no mutex is needed.
Each goroutine writes to a distinct range of the slice, which is safe. The built-in min needs Go 1.21 or later.
Serving is where Go is strongest. The handler below validates input, calls the model and returns JSON, using the Go 1.22 method-aware mux.
Test it with curl -d '{"x": 6}' localhost:8080/predict. For a real model, replace the formula with a call into an ONNX runtime binding or an HTTP call to an inference server, and add a timeout with context.
Not with the standard library alone. Use bindings to a runtime such as ONNX Runtime or TensorFlow, or call a separate inference server, and keep Go for the surrounding service.
Gonum is the main third-party numerical library for Go, with matrices, statistics, optimisation and plotting. It is the usual step after the standard library for numeric work.
Yes. net/http, encoding/json, context timeouts and goroutines are all you need to call hosted model APIs, stream responses and run requests concurrently.
For classical algorithms on modest data, it works. For deep learning, train in a framework built for it and use Go to serve the result.
The standard library gives Go the plumbing for ML and AI systems: dataset loading, simple algorithms, parallel preprocessing and fast HTTP serving. It has no tensors or neural network layers, so use Gonum for numerics and a runtime or remote service for deep learning models.