-
-
Save d4z3x/2df3d5b30aaa3263d33716ca3dee46f8 to your computer and use it in GitHub Desktop.
golang fnv example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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