Introduction: Go and Machine Learning — an Unexpected Duo
\n\nWhen it comes to machine learning (ML), Python with its ecosystem of NumPy, TensorFlow, and PyTorch is the first thing that comes to mind. However, Go (Golang) — a language created by Google for high-performance server-side solutions — is increasingly penetrating the ML sphere. In this article, we will examine how suitable Go is for machine learning tasks, where it is truly strong, and where its capabilities are limited. We will not build illusions: Go will not replace Python in Data Science, but it can become an excellent tool for production solutions.
\n\n1. Strengths of Go for ML Tasks
\n\n1.1. Performance and Concurrency
\n\nGo compiles to machine code and runs significantly faster than interpreted Python. This is critical for the inference stage — when a trained model is applied to new data. Built-in goroutines and channels make it easy to parallelize data processing without complex constructs.
\n\nExample: parallel feature preprocessing for a model:
\n\npackage main\n\nimport (\n "fmt"\n "sync"\n)\n\n// Function to preprocess a single feature\nfunc preprocessFeature(feature float64) float64 {\n // Normalization\n return (feature - 50.0) / 25.0\n}\n\nfunc main() {\n data := []float64{23.5, 67.8, 45.1, 89.2, 12.3}\n var wg sync.WaitGroup\n results := make([]float64, len(data))\n\n for i, val := range data {\n wg.Add(1)\n go func(idx int, v float64) {\n defer wg.Done()\n results[idx] = preprocessFeature(v)\n }(i, val)\n }\n wg.Wait()\n fmt.Println("Processed data:", results)\n}\n\n\n1.2. Ease of Deployment
\n\nA Go program compiles into a single binary file with no external dependencies. For ML models in production, this means you can package the model and all code into one executable file and run it on any server without installing Python, CUDA, or complex environments.
\n\n1.3. Reliability and Static Typing
\n\nGo's static typing helps catch errors at compile time rather than at runtime, which is critical for production systems. Many ML errors (e.g., passing a string instead of a number) are detected immediately.
\n\n2. Main Go Libraries for Machine Learning
\n\n2.1. Gorgonia — Neural Networks in Go
\n\nGorgonia is an analog of TensorFlow for Go. It allows you to create and train neural networks with automatic differentiation. The library supports CPU and GPU (via CUDA).
\n\nExample: simple linear regression with Gorgonia:
\n\npackage main\n\nimport (\n "fmt"\n "gorgonia.org/gorgonia"\n "gorgonia.org/tensor"\n)\n\nfunc main() {\n g := gorgonia.NewGraph()\n // Input data: x = [1, 2, 3]\n x := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(3, 1), gorgonia.WithInit(gorgonia.Zeroes()))\n // Weights: w\n w := gorgonia.NewMatrix(g, tensor.Float64, gorgonia.WithShape(1, 1), gorgonia.WithInit(gorgonia.GlorotN(1.0)))\n // y = x * w\n y, _ := gorgonia.Mul(x, w)\n // Run the graph\n vm := gorgonia.NewTapeMachine(g)\n vm.RunAll()\n fmt.Println("Result:", y.Value())\n}\n\n\n2.2. GoLearn — Classic ML
\n\nGoLearn provides implementations of popular algorithms: k-nearest neighbors, naive Bayes classifier, linear regression, random forest, and others. The library is easy to use and well-documented.
\n\nimport (\n "fmt"\n "github.com/sjwhitworth/golearn/base"\n "github.com/sjwhitworth/golearn/knn"\n)\n\nfunc main() {\n // Load data (CSV)\n data, _ := base.ParseCSVToInstances("iris.csv", true)\n // Create KNN classifier\n knn := knn.NewKnnClassifier("euclidean", "linear", 2)\n // Train\n knn.Fit(data)\n // Prediction (example)\n fmt.Println("Model trained")\n}\n