delve/_fixtures/setiterator.go
Alessandro Arzilli 582305a813
proc: for optimized functions allow .closureptr to not exist (#3808)
* proc: flag variables correctly when range-over-func stmts are used

Argument variables of a range-over-func body closure should be returned
flagged as normal local variables, not as function arguments.

Updates #3806

* proc: for optimized functions allow .closureptr to not exist

For optimized functions .closureptr is sometimes omitted from DWARF,
allow it to be 0 and try to recover the range-over-func stack by best
effort.

Fixes #3806
2024-09-18 14:16:34 -07:00

52 lines
830 B
Go

package main
import (
"fmt"
"iter"
"strconv"
"time"
)
func main() {
set := New[string]()
for i := 10; i < 100; i++ {
set.Add(strconv.Itoa(i))
}
PrintAllElements[string](set)
}
// Set holds a set of elements.
type Set[E comparable] struct {
m map[E]struct{}
}
// New returns a new [Set].
func New[E comparable]() *Set[E] {
return &Set[E]{m: make(map[E]struct{})}
}
// All is an iterator over the elements of s.
func (s *Set[E]) All() iter.Seq[E] {
return func(yield func(E) bool) {
for v := range s.m {
tmp := make([]byte, 1024)
str := string(tmp)
if !yield(v) {
return
}
go func() { println(str) }()
}
}
}
func (s *Set[E]) Add(v E) {
s.m[v] = struct{}{}
}
func PrintAllElements[E comparable](s *Set[E]) {
for v := range s.All() {
time.Sleep(100 * time.Second)
fmt.Println(v)
}
}