Skip to content

Instantly share code, notes, and snippets.

@d4z3x
Forked from lenage/fnv_example.go
Created December 23, 2020 07:11
Show Gist options
  • Save d4z3x/2df3d5b30aaa3263d33716ca3dee46f8 to your computer and use it in GitHub Desktop.
Save d4z3x/2df3d5b30aaa3263d33716ca3dee46f8 to your computer and use it in GitHub Desktop.
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