github.com/enetx/g@v1.0.80/examples/iter/iter_inspect.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/enetx/g"
     7  )
     8  
     9  func main() {
    10  	// Create a new ordered map with integer keys and string values
    11  	mo := g.NewMapOrd[int, string]()
    12  	mo.Set(0, "aa").
    13  		Set(1, "bb").
    14  		Set(2, "cc").
    15  		Set(3, "dd").
    16  		Set(4, "ee").
    17  		Set(5, "ff").
    18  		Set(6, "gg").
    19  		Iter().
    20  		StepBy(2).                                                        // Iterate with a step size of 2
    21  		Exclude(func(k int, _ string) bool { return k == 4 }).            // Exclude entry with key 4
    22  		Inspect(func(k int, v string) { fmt.Println("~inspect", k, v) }). // Inspect each entry
    23  		Map(func(k int, v string) (int, string) { return k, v + v }).
    24  		// Map values to concatenate them with themselves
    25  		Collect(). // Collect the resulting ordered map
    26  		Print()    // Print the ordered map
    27  }