Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
golang fnv example
package main
import (
"bufio"
"fmt"
"hash/fnv"
"io"
"os"
)
func fingerprint(b []byte) uint64 {
hash := fnv.New64a()
hash.Write(b)
return hash.Sum64()
}
func main() {
var m = make(map[uint64][]byte)
f, err := os.Open("./words.txt")
if err != nil {
fmt.Printf("error opening file: %v\n", err)
os.Exit(1)
}
defer f.Close()
r := bufio.NewReader(f)
for {
line, _, err := r.ReadLine()
if err == io.EOF {
break
}
n := fingerprint(line)
if m[n] == nil {
m[n] = line
} else {
fmt.Printf("%s: %d\n", line, n)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.