Simple Neural Network Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Simple Neural Network runs in O(n*w) time and O(w) space, in Go as in every other language.
- ✓math/rand/v2, the modern generator added in Go 1.22, is the real answer; the older math/rand is now considered legacy for anything reproducibility-sensitive.
- ✓Define type Layer struct { Weights, Biases []float64; In, Out int } and a constructor NewLayer(in, out int, r *rand.Rand) Layer that takes the generator explicitly, the same 'pass the RNG in, do not read a global' shape Rust favors.
- ✓The Go trap: make([]float64, n) returns an all-zero slice of LENGTH n, not length 0, which silently satisfies every subsequent len(weights) check as though initialization already happened.
- ✓A *rand.Rand created via rand.New(rand.NewPCG(...)) is NOT safe for concurrent use by multiple goroutines without external locking, unlike the legacy package-level rand.Float64(), which is internally locked/thread-safe.
What Simple Neural Network actually is
Simple Neural Network in Go: what you actually reach for
Go implementation
package main
import (
"math"
"math/rand/v2"
)
type Layer struct {
Weights, Biases []float64
In, Out int
}
func newLayer(in, out int, r *rand.Rand) Layer {
scale := math.Sqrt(2.0 / float64(in)) // He init
weights := make([]float64, in*out)
for i := range weights {
weights[i] = r.NormFloat64() * scale // built-in Gaussian, no Box-Muller needed
}
return Layer{Weights: weights, Biases: make([]float64, out), In: in, Out: out}
}
func buildNetwork(sizes []int, r *rand.Rand) []Layer {
layers := make([]Layer, 0, len(sizes)-1)
for i := 0; i < len(sizes)-1; i++ {
layers = append(layers, newLayer(sizes[i], sizes[i+1], r))
}
return layers
}
func (l *Layer) forward(input []float64) []float64 {
out := make([]float64, l.Out)
for o := 0; o < l.Out; o++ {
sum := l.Biases[o]
for i := 0; i < l.In; i++ {
sum += input[i] * l.Weights[o*l.In+i]
}
if sum < 0 {
sum = 0 // ReLU
}
out[o] = sum
}
return out
}
func main() {
r := rand.New(rand.NewPCG(42, 1024)) // seeded: not safe to share across goroutines
net := buildNetwork([]int{784, 128, 10}, r)
_ = net
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(n*w)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Simple Neural Network?
math/rand/v2, the modern generator added in Go 1.22, is the real answer; the older math/rand is now considered legacy for anything reproducibility-sensitive. rand.NewPCG(seed1, seed2) seeds a source, rand.New wraps it into a *rand.Rand, and (*rand.Rand).NormFloat64() gives a standard-normal sample directly, no hand-rolled Box-Muller needed.
What is the most common Go mistake when implementing Simple Neural Network?
make([]float64, n) returns an all-zero slice of LENGTH n, not length 0, which silently satisfies every subsequent len(weights) check as though initialization already happened. Building weights by appending sampled values on top of that, weights = append(weights, sample), instead of assigning by index, or instead of starting from make([]float64, 0, n), produces a slice of length 2n, half zeros and half real weights, with no error at any point, because append grows past the slice's existing length rather than filling it.
How does Simple Neural Network actually perform in Go?
A *rand.Rand created via rand.New(rand.NewPCG(...)) is NOT safe for concurrent use by multiple goroutines without external locking, unlike the legacy package-level rand.Float64(), which is internally locked/thread-safe. Fanning the initialization of several layers out across goroutines sharing one *rand.Rand is a data race the race detector will catch; the fix is one generator per goroutine or a mutex around sampling, either of which cuts into the real-world parallel speedup on what is, for a small network, already a cheap one-time cost. Time complexity is O(n*w) and space is O(w).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialTo experiment offline, set up a local language model in a few commands using the guides on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.