Go's standard library is a good fit for the data-handling parts of scientific and research work: reading CSV and JSON, basic statistics with math and slices, exact arithmetic with math/big, random sampling with math/rand/v2, and parallel processing with goroutines. It has no matrix, dataframe or plotting package, so heavy numerical work usually moves to a third-party module.
This guide shows what the standard library does well, with complete examples, and where to reach for Gonum or another module instead.
| Need | Standard library | Limits |
|---|---|---|
| Read and write tabular data | encoding/csv | Streams records; no type inference |
| Structured data exchange | encoding/json, encoding/gob | JSON numbers decode to float64 by default |
| Basic math | math, math/bits, math/cmplx | Scalars only |
| Exact numbers | math/big | Arbitrary-size integers, rationals, floats |
| Random numbers | math/rand/v2 | Not cryptographically secure |
| Sorting and searching | slices, sort | Generic helpers since Go 1.21 |
| Parallelism | goroutines, sync, context | You design the pipeline |
| Missing | matrices, dataframes, plots, FFT, linear solvers | Use Gonum or similar |
Stream the file record by record instead of calling ReadAll, so memory stays flat for large datasets. This program sums and averages the second column of a CSV and reports bad rows instead of silently skipping them.
For a file with three valid rows a,10, b,20, c,30, the output is:
Since Go 1.21 the slices and cmp packages and generics remove the need for sort.Float64s boilerplate. A small generic helper set covers mean, median and standard deviation.
Cloning before sorting avoids mutating the caller's slice. The helpers handle empty input poorly, so guard against len(xs) == 0 in real code. For numerically sensitive work such as large sums, consider Welford's algorithm or Kahan summation, because naive floating-point accumulation loses precision.
Floating-point types cannot represent many decimal values exactly. math/big provides arbitrary-precision integers (big.Int), rationals (big.Rat) and floats (big.Float).
Use math/rand/v2 (added in Go 1.22) for simulations. It is seedable for reproducible runs through rand.New(rand.NewPCG(seed1, seed2)). A Monte Carlo estimate of pi:
Never use math/rand for security-sensitive randomness; that is the job of crypto/rand. A *rand.Rand is not safe for concurrent use, so give each goroutine its own.
Goroutines make it simple to split independent work across CPU cores. This example sums chunks of a slice in parallel and combines the partial results.
Each goroutine writes to its own slot in partial, so no lock is needed. Since Go 1.22 loop variables are per-iteration, so capturing w, lo and hi in the closure is safe. Profile with go test -bench and pprof before assuming parallelism helps, because small inputs are faster on one core.
Add a module when you need matrices, linear algebra, optimisation, distributions or plotting. Gonum (gonum.org/v1/gonum) is the established numerical library for Go. For large-scale machine learning or heavy array work, Python with NumPy remains the mainstream choice, and Go is often used for the services and pipelines around it.
It is good for data pipelines, simulations, command-line tools and services that need speed and easy deployment. It lacks the mature array, dataframe and plotting ecosystem of Python, R or Julia.
Use plain Go with slices for simple means and medians, and Gonum's stat package for distributions, regression and correlation.
Binary floating point cannot represent many decimal fractions exactly, and rounding accumulates. Use math/big, compensated summation, or integer units such as cents when exactness matters.
No. Use crypto/rand for keys, tokens and anything an attacker must not predict.
The standard library handles data input and output, descriptive statistics, exact arithmetic, reproducible random sampling and parallel processing. Stream CSV rather than loading it whole, use slices and generics for helpers, prefer math/rand/v2, and bring in Gonum when you need linear algebra or advanced statistics.