I wrote a small Go tool[1] that extracts a color palette from an image using either median or mean quantizer.
The quantizer code[2] is well documented and worth reading if you're looking at doing this in Go.
[1] https://github.com/andrewgleave/color-extract [2] https://github.com/soniakeys/quant