find the standard deviation of all values in a map in go

To find the standard deviation of all values in a map in Go, we can use the math and statistics packages. Here's an example code snippet:

main.go
package main

import (
	"fmt"
	"math"
	"github.com/montanaflynn/stats"
)

func main() {
	m := map[string]float64{
		"a": 1.0,
		"b": 2.0,
		"c": 3.0,
	}

	var values []float64
	for _, v := range m {
		values = append(values, v)
	}

	// calculate standard deviation using stats package
	stdDev, _ := stats.StdDev(values)

	fmt.Printf("Standard deviation: %v\n", stdDev)
}
371 chars
26 lines

In this code, we first create a map with some float64 values. We then create an empty slice called values, and loop through each value in the map, appending it to the slice. We then pass the slice to the stats.StdDev function from the statistics package, which returns the standard deviation. We then print the result to the console. Note that we use _ to ignore the error returned by the stats.StdDev function, since we don't need it for this example.

gistlibby LogSnag