github.com/aristanetworks/gomap@v0.0.0-20240103001659-f6b0e31fb1a7/example_test.go (about) 1 // Modifications copyright (c) Arista Networks, Inc. 2022 2 // Underlying 3 // Copyright 2014 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package gomap_test 8 9 import ( 10 "bytes" 11 "fmt" 12 "hash/maphash" 13 14 "github.com/aristanetworks/gomap" 15 ) 16 17 func ExampleMap_Iter() { 18 m := gomap.New( 19 func(a, b string) bool { return a == b }, 20 maphash.String, 21 gomap.KeyElem[string, string]{"Avenue", "AVE"}, 22 gomap.KeyElem[string, string]{"Street", "ST"}, 23 gomap.KeyElem[string, string]{"Court", "CT"}, 24 ) 25 26 for i := m.Iter(); i.Next(); { 27 fmt.Printf("The abbreviation for %q is %q", i.Key(), i.Elem()) 28 } 29 } 30 31 func ExampleNew() { 32 // Some examples of different maps: 33 34 // Map that uses []byte as the key type 35 byteSliceMap := gomap.New[[]byte, int](bytes.Equal, maphash.Bytes) 36 byteSliceMap.Set([]byte("hello"), 42) 37 38 // Map that uses map[string]struct{} as the key type 39 stringSetEqual := func(a, b map[string]struct{}) bool { 40 if len(a) != len(b) { 41 return false 42 } 43 for k := range a { 44 _, ok := b[k] 45 if !ok { 46 return false 47 } 48 } 49 return true 50 } 51 stringSetHash := func(seed maphash.Seed, ss map[string]struct{}) uint64 { 52 var sum uint64 53 for s := range ss { 54 // combine hashes with addition so that iteration order does not matter 55 sum += maphash.String(seed, s) 56 } 57 return sum 58 } 59 stringSetMap := gomap.New[map[string]struct{}, int](stringSetEqual, stringSetHash) 60 stringSetMap.Set(map[string]struct{}{"foo": {}, "bar": {}}, 42) 61 }