Golang hash.Hash64 type examples
package hash
Hash64 is the common interface implemented by all 64-bit hash functions.
Golang hash.Hash64 type usage examples
Example 1:
func hashFile(name string, h interface{}, r io.Reader) error {
w, ok := h.(hash.Hash)
if ok {
_, err := io.Copy(w, r)
if err != nil {
return err
}
switch hashType := h.(type) {
case hash.Hash32:
{
result := hashType.Sum32()
fmt.Fprintf(os.Stdout, "%s = 0x%x\n", name, result)
}
case hash.Hash64: // <-- HERE
{
result := hashType.Sum64()
fmt.Fprintf(os.Stdout, "%s = 0x%x\n", name, result)
}
default:
{
return ErrInvalidHashType
}
}
}
return nil
}
Example 2:
package main
import (
"fmt"
"hash"
)
// demo for hash.Hash64 purpose.
// you need to write your own Write(), reset(), Size, Blocksize and Sum64 inner functions
// fakeHash64 is a dummy Hash64 that always returns 0.
type fakeHash64 struct {
hash.Hash64
}
func (fakeHash64) Write(p []byte) (int, error) { return len(p), nil }
func (fakeHash64) Sum64() uint64 { return 0 }
func main() {
var h hash.Hash64 = fakeHash64{}
h.Write([]byte("abc123"))
fmt.Printf("%d \n", h.Sum64())
}
Reference :
Advertisement
Something interesting
Tutorials
+8.6k Golang : Another camera capture GUI application with GTK and OpenCV
+13.9k Golang : Get current time
+29.2k Golang : missing Git command
+6.9k Golang : Fibonacci number generator examples
+8.6k Golang : Set or add headers for many or different handlers
+4.6k Mac OSX : Get disk partitions' size, type and name
+13.7k Golang : Image to ASCII art example
+9.2k Golang : does not implement flag.Value (missing Set method)
+6.9k Golang : Normalize email to prevent multiple signups example
+22.9k Golang : Gorilla mux routing example
+12k Golang : Convert a rune to unicode style string \u
+26.4k Golang : Convert(cast) string to uint8 type and back to string