Go can be used for support vector machine (SVM) models, but it has no standard SVM package and no equivalent of scikit-learn's SVC. You have three realistic options: write a linear SVM yourself (it is short), use a library that wraps libsvm or liblinear, or train in Python and run the exported model in Go. For most production work, train with scikit-learn and serve the model from Go.
This article explains what an SVM does, implements a working linear SVM with stochastic gradient descent in Go, and describes how to use library and ONNX based approaches for kernels and larger problems.
An SVM is a supervised classifier that finds the hyperplane separating two classes with the largest possible margin. Points closest to the boundary are the support vectors. The decision function for a linear SVM is w . x + b, and the sign gives the class.
Two ideas matter for practical use:
max(0, 1 - y * (w . x + b)) plus a penalty on the size of w. The regularisation strength controls the trade-off between margin and errors.Always scale features before training, because the margin depends on distances.
| Option | Kernels | Effort | Notes |
|---|---|---|---|
| Hand-written linear SVM | Linear only | Low | Good for learning and small services |
| libsvm wrappers (for example GoLearn's SVM package, pure Go libsvm ports) | Linear, polynomial, RBF, sigmoid | Medium | Check each project's maintenance and cgo requirements |
| Train in Python, export to ONNX | All scikit-learn kernels | Low | Run inference with ONNX Runtime bindings from Go |
| Model server over gRPC or HTTP | All | Low | Keeps Python out of the Go binary |
GoLearn offers a scikit-learn style API and includes SVM support built on libsvm. Its API and build requirements change over time, so read the current documentation before adopting it.
The Pegasos algorithm trains a linear SVM with stochastic sub-gradient descent. At step t it picks one random sample, uses a learning rate of 1 / (lambda * t), shrinks the weights for regularisation and, if the sample violates the margin, nudges the weights toward it.
The program below generates two Gaussian clusters, trains on 150 samples and evaluates on 50 held-out samples.
Output with the fixed seed:
The clusters are well separated, so the accuracy is perfect. The bias is updated with a small fixed step and is not regularised, which is a simple approach that works for this demonstration. A production implementation would add a constant feature for the bias, average the weights over iterations, stop on a convergence check and use a larger, standardised feature set.
Most teams train SVMs with scikit-learn, where SVC supports kernels, class weights and probability calibration, and LinearSVC handles large linear problems. To use the model from Go:
w . x + b directly.For a linear model the JSON route is simple and has no native dependency. For kernel SVMs, ONNX or a model server is safer than reimplementing the kernel evaluation, because you must keep the support vectors, dual coefficients and kernel parameters consistent.
C (or lambda in the Pegasos formulation) and, for an RBF kernel, gamma, on a logarithmic grid.No. Use a third-party library, write a linear SVM yourself, or run a model trained elsewhere.
Yes, through libsvm-based libraries or by running an ONNX model. A hand-written linear SVM does not support kernels.
Not usually. scikit-learn calls optimised C and C++ code (libsvm and liblinear), so the training speed is similar or better than a hand-written Go version. Go's advantage is in serving.
For very large datasets, gradient boosted trees or neural networks often perform better, and kernel SVMs become slow beyond tens of thousands of samples.
Go can run SVMs, either with a short hand-written linear implementation or with libsvm-based libraries. For kernels, tuning and large data, train in Python and export the model to Go through ONNX, JSON weights or a model server. Scale your features, tune regularisation and verify Go predictions against the Python reference.