Machine Learning in Go (Golang): Libraries and Tools for Development
Although Python remains the dominant language in the world of machine learning (ML), Go (Golang) is gaining popularity due to its performance, ease of deployment, and excellent support for concurrency. Go is ideal for building high-load ML services, batch data processing, and embedding models into a production environment. In this article, we will review the key libraries for machine learning in Go, their capabilities, and usage examples.
Why Go for Machine Learning?
Go does not try to replace Python in the research phase, but it offers unique advantages at the model deployment stage:
- Performance: A compiled language, close in speed to C/C++, without interpreter overhead.
- Ease of Deployment: A single binary without dependencies — ideal for microservices and containerization.
- Built-in Concurrency: Goroutines and channels allow efficient data stream processing and parallel model training.
- Static Typing: Fewer runtime errors, which is critical for production systems.
1. Gorgonia — Deep Learning Framework
Gorgonia is the Go equivalent of TensorFlow and PyTorch. It provides tools for building and training neural networks, including automatic differentiation, GPU support (via CUDA), and computation graph optimization.
package main
import ( "fmt" "gorgonia.org/gorgonia" "gorgonia.org/tensor")
func main() { g := gorgonia.NewGraph()
// Define input data (2x3 matrix) x := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithName("x"), gorgonia.WithShape(2, 3))
// Define weights (3x2 matrix) w := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithName("w"), gorgonia.WithShape(3, 2))
// Matrix multiplication operation a, _ := gorgonia.Mul(x, w)
// Apply sigmoid sigmoid := gorgonia.Must(gorgonia.Sigmoid(a))
// Create a VM to execute the graph machine := gorgonia.NewTapeMachine(g) defer machine.Close()
// Fill with data gorgonia.Let(x, tensor.New(tensor.WithBacking([]float64{1, 2, 3, 4, 5, 6}), tensor.WithShape(2, 3))) gorgonia.Let(w, tensor.New(tensor.WithBacking([]float64{0.1, 0.2, 0.3, 0.4, 0.5, 0.6}), tensor.WithShape(3, 2)))
// Execute the graph if err := machine.RunAll(); err != nil { fmt.Println(err) return }
fmt.Println("Sigmoid result:", sigmoid.Value())}When to use: For creating custom neural networks, research and experiments when full control over the computation graph is needed.
2. GoLearn — General Purpose Library
GoLearn provides a wide range of machine learning algorithms: classification (kNN, Random Forest), regression, clustering (k-means), and data preprocessing. It follows the principles of scikit-learn, making it intuitive for developers familiar with Python.
package main
import ( "fmt" "github.com/sjwhitworth/golearn/base" "github.com/sjwhitworth/golearn/knn" "github.com/sjwhitworth/golearn/evaluation")
func main() { // Load data (CSV) rawData, err := base.ParseCSVToInstances("iris.csv", true) if err != nil { fmt.Println(err) return }
// Create k-NN classifier (k=3, euclidean distance) cls := knn.NewKnnClassifier("euclidean", 3)
// Split into train/test (80/20) trainData, testData := base.InstancesTrainTestSplit(rawData, 0.20)
// Train the model cls.Fit(trainData)
// Predict predictions, err := cls.Predict(testData) if err != nil { fmt.Println(err)