Machine Learning in Go (Golang): Overview of Libraries and Tools
Although Python remains the dominant language in Data Science and machine learning, Go (Golang) is gaining popularity due to its performance, ease of deployment, and excellent support for concurrency. If you need to embed an ML model into a high-load microservice, process data in real-time, or create a lightweight pipeline without heavy dependencies — Go can be an excellent choice. In this article, we will review key libraries for machine learning in Go, their capabilities, and provide practical examples.
Why Go for Machine Learning?
- Performance: Go compiles to native code, providing a speed boost compared to interpreted languages.
- Ease of Deployment: A single binary — no dependencies on the target machine.
- Built-in Concurrency: Goroutines and channels are ideal for parallel data processing and model training.
- Ecosystem: Although there are fewer ML libraries than in Python, they are actively developed and cover most basic tasks.
1. Gorgonia — Deep Learning Framework
Gorgonia is the Go counterpart of TensorFlow and PyTorch. It provides a low-level API for creating and training neural networks, supports automatic differentiation, and works with GPUs via CUDA.
Key Features:
- Building computational graphs.
- Automatic gradient computation (backpropagation).
- Support for convolutional, recurrent, and fully connected layers.
- Integration with CUDA for GPU acceleration.
Example of simple linear regression:
package main
import ( "fmt" "gorgonia.org/gorgonia" "gorgonia.org/tensor")
func main() { g := gorgonia.NewGraph() // Input data: x and y x := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(3, 1), gorgonia.WithName("x")) y := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(3, 1), gorgonia.WithName("y")) // Model parameters: weight w and bias b w := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(1, 1), gorgonia.WithName("w"), gorgonia.WithInit(gorgonia.GlorotN(1.0))) b := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(1, 1), gorgonia.WithName("b"), gorgonia.WithInit(gorgonia.Zeroes())) // Forward pass: y_pred = x * w + b pred := gorgonia.Must(gorgonia.Add(gorgonia.Must(gorgonia.Mul(x, w)), b)) // Loss function: MSE loss := gorgonia.Must(gorgonia.Mean(gorgonia.Must(gorgonia.Square(gorgonia.Must(gorgonia.Sub(pred, y)))))) // Create VM and train vm := gorgonia.NewTapeMachine(g) // ... training code with gradient descent fmt.Println("Model created, loss:", loss)}When to use: for creating custom neural networks, research projects where full control over the computation graph is needed.
2. GoLearn — Classical ML Library
GoLearn (golearn) is the Go counterpart of scikit-learn. It provides ready-made implementations of algorithms: k-nearest neighbors (KNN), naive Bayes classifier, decision trees, linear regression, support vector machines (SVM), and others.
Advantages:
- Convenient API, similar to sklearn.
- Built-in evaluation metrics (accuracy, precision, recall, F1).
- Support for cross-validation and pipelines.
Example of classification with KNN:
package main
import ( "fmt" "github.com/sjwhitworth/golearn/base" "github.com/sjwhitworth/golearn/knn" "github.com/sjwhitworth/golearn/evaluation")
func main() { // Load data (CSV) data, er