Source file src/hash/maphash/example_test.go

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package maphash_test
     6  
     7  import (
     8  	"fmt"
     9  	"hash/maphash"
    10  )
    11  
    12  func Example() {
    13  	// The zero Hash value is valid and ready to use; setting an
    14  	// initial seed is not necessary.
    15  	var h maphash.Hash
    16  
    17  	// Add a string to the hash, and print the current hash value.
    18  	h.WriteString("hello, ")
    19  	fmt.Printf("%#x\n", h.Sum64())
    20  
    21  	// Append additional data (in the form of a byte array).
    22  	h.Write([]byte{'w', 'o', 'r', 'l', 'd'})
    23  	fmt.Printf("%#x\n", h.Sum64())
    24  
    25  	// Reset discards all data previously added to the Hash, without
    26  	// changing its seed.
    27  	h.Reset()
    28  
    29  	// Use SetSeed to create a new Hash h2 which will behave
    30  	// identically to h.
    31  	var h2 maphash.Hash
    32  	h2.SetSeed(h.Seed())
    33  
    34  	h.WriteString("same")
    35  	h2.WriteString("same")
    36  	fmt.Printf("%#x == %#x\n", h.Sum64(), h2.Sum64())
    37  }
    38  

View as plain text