Go is not a replacement for Python in exploratory machine learning, but it is a strong choice for the data pipelines, model serving and production services around a model. The standard library covers data ingestion (encoding/csv, encoding/json), basic math (math, sort, slices) and concurrency; for statistics, matrices and training you add libraries such as Gonum, Gorgonia or an ONNX runtime binding.
The standard library has no statistics, matrix or model-training package. Older articles, including the first version of this one, claimed a math/stat package exists. It does not; use gonum.org/v1/gonum/stat instead.
| Task | Standard library package | Notes |
|---|---|---|
| Read tabular data | encoding/csv | Streaming reader, handles quoting |
| Exchange data | encoding/json, encoding/gob | JSON for APIs, gob for Go-to-Go |
| Parse numbers | strconv | ParseFloat with explicit error handling |
| Basic math | math, math/rand/v2 | Seeded generators for reproducible shuffles |
| Sort, search, dedupe | sort, slices, maps | Generic helpers since Go 1.21 |
| Parallel work | goroutines, sync, context | Worker pools for feature extraction |
What is missing is everything numerical: data frames, linear algebra, optimisers and automatic differentiation. That gap is filled by third-party modules.
Read rows one at a time with Read rather than ReadAll so a large file does not have to fit in memory. The following program parses a two-column file (feature,label) into typed slices and reports bad rows instead of panicking.
FieldsPerRecord = 2 makes the reader reject malformed rows, and errors.Join (Go 1.20+) reports both parse failures at once.
Gonum is the de facto numerical library for Go. Its stat package replaces the non-existent math/stat.
StdDev returns the sample standard deviation (n-1), which is why the value is 1.58 and not the population value of 1.41. The nil argument means unweighted. For matrices use gonum.org/v1/gonum/mat.
| Library | Good for | Caveat |
|---|---|---|
| Gonum | Statistics, linear algebra, optimisation | Numerical building blocks, not a model zoo |
| Gorgonia | Graph-based neural networks with autodiff | Lower level than PyTorch; small community |
| GoLearn | Classic algorithms (trees, k-NN, linear models) | Sparse maintenance; check recent commits first |
| ONNX Runtime bindings | Running models trained in Python | Needs the native runtime library, so cgo or shared libraries |
| Hosted model APIs | LLM and vision features | Plain HTTPS from net/http |
Some libraries named in older posts, such as a "Fuego" ML package, do not exist as described, and the GoLearn call signatures shown there were invented. Always read the current README and examples of a library before depending on it.
Most teams train with PyTorch, scikit-learn or XGBoost, export to ONNX, and load the model in a Go service. Go then handles the HTTP or gRPC layer, batching, timeouts and observability, which is where its strengths are.
Feature extraction is usually embarrassingly parallel. A bounded worker pool keeps CPU use predictable:
Each goroutine writes to its own slice range, so no mutex is needed. Since Go 1.22 each loop iteration also has its own variables, which removes the old closure-capture bug.
Use math/rand/v2 with an explicit seed (rand.New(rand.NewPCG(1, 2))) for shuffles and train/test splits, and keep the seed in your run metadata.
For exploration, plotting and training, not yet: the ecosystem is far smaller. For data engineering, inference services and anything latency or deployment sensitive, Go is a good fit.
Third-party options exist, and Gonum offers mat.Dense for numeric tables, but none is as complete as pandas. Many teams process rows as typed structs instead.
Not natively. GPU work is done through bindings to CUDA-based runtimes such as ONNX Runtime, or by calling a separate serving process.
Wrap the model in a small HTTP or gRPC service, or export it to ONNX and run it in-process through an ONNX Runtime binding.
The Go standard library handles ingestion, parsing, concurrency and serving; it does not include statistics or ML algorithms. Use Gonum for numerics, Gorgonia or ONNX Runtime for models, verify every third-party library before relying on it, and let Go do what it does best: reliable, concurrent production services around a model.