Yes, you can develop artificial neural networks in Go, but it is not the usual choice for research. Python with PyTorch or TensorFlow dominates training because of its libraries and GPU tooling. Go is strongest around the model: serving inference, running exported models, building data pipelines and APIs, and teaching how networks work with plain code.
This article shows a neural network written from scratch in Go, surveys the libraries that exist today, and explains the practical split between training in Python and deploying in Go.
| Strength | Limitation |
|---|---|
| Fast compiled code and simple deployment | Far smaller machine learning ecosystem than Python |
| Goroutines for parallel data loading and batching | Limited GPU and automatic differentiation tooling |
| Static binaries for inference services | Few pretrained model hubs and research examples |
| Strong typing and easy testing | No built-in tensor type; you rely on libraries |
For research and large-scale training, use Python. For production inference and services, Go is an excellent host.
The program below trains a small network with two inputs, four hidden sigmoid units and one output to learn the XOR function. XOR is not linearly separable, so it needs the hidden layer. It uses only the standard library (math/rand/v2 needs Go 1.22 or newer) and plain stochastic gradient descent with backpropagation.
Expected output:
The Forward method computes activations layer by layer. Train computes the output error, multiplies by the sigmoid derivative, and propagates the gradient back to the hidden weights. The seeded generator makes the run reproducible, and exact digits can differ slightly with another seed or Go version.
| Library | Purpose |
|---|---|
Gonum (gonum.org/v1/gonum) | Matrices, linear algebra, statistics and optimisation; the numerical foundation |
Gorgonia (gorgonia.org/gorgonia) | Computation graphs with automatic differentiation for building and training networks |
| GoMLX | Machine learning framework for Go built on the XLA compiler, with GPU and TPU support |
onnxruntime_go | Bindings to ONNX Runtime for running exported models |
| GoLearn | Classical machine learning such as decision trees and k-nearest neighbours, not deep learning |
Check each project's repository for recent activity and current API before depending on it, because the Go machine learning ecosystem moves slowly and some projects are lightly maintained.
The most common production pattern is a split:
This keeps your research workflow intact while giving you a small, fast and observable inference service. Alternatively, your Go service can call a hosted model through an HTTP API and add retries, timeouts and caching around it.
Goroutines do not make matrix maths faster on their own, but they help around it:
Guard shared weights with a mutex or treat a loaded model as read-only during inference. Run go test -race to catch races.
You can with Gorgonia or GoMLX, but tooling, documentation and pretrained models are far richer in Python. Most teams train elsewhere and deploy in Go.
Plain Go loops are faster than plain Python loops, but Python libraries call optimised C, C++ and GPU kernels. For real models the underlying kernels matter more than the host language.
Export the model to ONNX and load it with an ONNX Runtime binding such as onnxruntime_go. Verify that every operator in your model is supported.
Yes, once. Writing forward and backward passes by hand teaches how gradients flow, and then you can switch to a library.
Go can build neural networks from scratch and has libraries such as Gonum, Gorgonia and GoMLX, but Python remains the main training environment. Use Go where it shines: concurrent data pipelines, inference services built on ONNX models and simple, deployable binaries. Treat library maturity as a selection criterion and test your model end to end.