Skip to content

Instantly share code, notes, and snippets.

@LaPingvino
Last active September 23, 2018 01:02
Show Gist options
  • Save LaPingvino/4b3bb144b367c773af99bb1a25542f22 to your computer and use it in GitHub Desktop.
Save LaPingvino/4b3bb144b367c773af99bb1a25542f22 to your computer and use it in GitHub Desktop.
Reads text from stdin, outputs CSV-formatted list of every unicode character (rune) in the file together with a number that says how much it appears
package main
// Because this code is so short and obvious, it should be considered public domain
import "os"
import "io/ioutil"
import "encoding/csv"
import "strconv"
func main() {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
inputs := string(input)
output := map[rune]int{}
for _, c := range inputs {
output[c]++
}
outputc := csv.NewWriter(os.Stdout)
for k, v := range output {
outputc.Write([]string{string(k), strconv.Itoa(v)})
}
outputc.Flush()
if outputc.Error() != nil {
panic(outputc.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment