Created
March 31, 2017 01:55
-
-
Save lenage/e8161cdfc461bf968f8687bbeae51dee to your computer and use it in GitHub Desktop.
golang fnv example
This file contains hidden or 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