Skip to content

Instantly share code, notes, and snippets.

@MilosSimic
Forked from lenage/fnv_example.go
Created January 21, 2019 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MilosSimic/38c6601e5bfb6a58151ebef1249ba9eb to your computer and use it in GitHub Desktop.
Save MilosSimic/38c6601e5bfb6a58151ebef1249ba9eb 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