github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/hash/maphash/example_test.go (about) 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 "github.com/shogo82148/std/fmt" 9 "github.com/shogo82148/std/hash/maphash" 10 ) 11 12 func Example() { 13 // ゼロのハッシュ値は有効で使用準備が整っています。初期シードの設定は必要ありません。 14 var h maphash.Hash 15 16 // 文字列をハッシュに追加し、現在のハッシュ値を表示します。 17 h.WriteString("hello, ") 18 fmt.Printf("%#x\n", h.Sum64()) 19 20 // 追加データをバイト配列の形式で追加します。 21 h.Write([]byte{'w', 'o', 'r', 'l', 'd'}) 22 fmt.Printf("%#x\n", h.Sum64()) 23 24 // Resetは、以前にハッシュに追加されたすべてのデータを破棄しますが、 25 // シードは変更しません。 26 h.Reset() 27 28 // 新しいハッシュh2を作成するために、SetSeedを使用し、hと完全に同じ動作をします。 29 var h2 maphash.Hash 30 h2.SetSeed(h.Seed()) 31 32 h.WriteString("same") 33 h2.WriteString("same") 34 fmt.Printf("%#x == %#x\n", h.Sum64(), h2.Sum64()) 35 }