Skip to content

Instantly share code, notes, and snippets.

@florianl
Created November 5, 2022 11:49
Show Gist options
  • Save florianl/68fcadaa8438be5a2386d43e775a1974 to your computer and use it in GitHub Desktop.
Save florianl/68fcadaa8438be5a2386d43e775a1974 to your computer and use it in GitHub Desktop.
TestPerCPUIterateClose
func TestPerCPUIterateClose(t *testing.T) {
arr, err := NewMap(&MapSpec{
Type: PerCPUArray,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
})
if err != nil {
panic(err)
}
first := []uint32{4, 5}
if err := arr.Put(uint32(0), first); err != nil {
panic(err)
}
second := []uint32{2, 8}
if err := arr.Put(uint32(1), second); err != nil {
panic(err)
}
var values []uint32
if err := arr.Lookup(uint32(0), &values); err != nil {
panic(err)
}
fmt.Println("First two values:", values[:2])
var (
key uint32
entries = arr.Iterate()
)
for entries.Next(&key, &values) {
// NB: sum can overflow, real code should check for this
var sum uint32
for _, n := range values {
sum += n
}
fmt.Printf("Sum of %d: %d\n", key, sum)
arr.Close()
}
if err := entries.Err(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment