Go's standard library does not include machine learning algorithms, but it covers the work around them: reading data with encoding/csv and encoding/json, basic maths with math, concurrency with goroutines, and serving models over net/http. For the numerical and model-training side you add libraries such as Gonum, or you call models that run elsewhere through an HTTP API or an inference runtime.
In practice, Go is used for the production half of AI systems: data pipelines, inference services, and gateways to hosted or local models. Most model training is still done in Python.
| Package | Use in an ML workflow |
|---|---|
encoding/csv, encoding/json | Load and export datasets and model inputs |
strconv | Parse text fields into numbers |
math, math/rand/v2 | Basic maths, sampling and shuffling |
sort, slices, maps | Ordering, searching and grouping data |
sync, context | Parallel processing, cancellation and timeouts |
net/http | Serve predictions and call model APIs |
testing | Unit tests and benchmarks for feature code |
There are no matrices, tensors, gradient descent or neural network layers in the standard library. Anything beyond statistics on slices needs a third-party package.
This program reads a CSV file with a header row and returns numeric rows. Unlike a quick script, it checks every error instead of ignoring parse failures.
Reading record by record keeps memory use flat for large files, which ReadAll does not.
You can write mean and variance with only math:
For anything larger, Gonum is the standard numerical library for Go. Its stat package already provides these functions, plus regression:
The example fits a straight line by least squares. Gonum's mat package adds dense matrices and linear algebra for larger models.
Preprocessing is often CPU-bound and splits cleanly across chunks. This version gives each worker its own slice and writes results by index, so no locking is needed.
The slices do not overlap, so there is no data race. Run go test -race on code like this to confirm.
encoding/json and net/http, always with a context timeout. This is the most common pattern for large language models.Be careful with older advice. Gorgonia, a deep learning library for Go, exists but is niche and its API has changed, and Python embedding libraries such as go-python are dated. Most teams now prefer a network call or ONNX over embedding Python in Go.
You can, with libraries such as Gorgonia, but the ecosystem is much smaller than Python's. Most teams train in Python and deploy inference in Go.
Go is fast for data handling and serving. It lacks the GPU-optimised tensor libraries that Python frameworks wrap, so heavy training is better done elsewhere.
Gonum is the most established. It provides matrices, linear algebra, statistics, optimisation and plotting helpers.
Send a JSON request to the provider's HTTP API with net/http, set a timeout through context, and decode the response with encoding/json. Many providers also publish official Go SDKs.
The standard library handles data loading, basic statistics, concurrency and serving, but not model training. Add Gonum for numerics, and use Go where it is strongest: pipelines, inference services and gateways to models trained and hosted elsewhere.