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 :

http://golang.org/pkg/hash/#Hash64

Advertisement