github.com/enetx/g@v1.0.80/examples/slices/slice_funcs.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/enetx/g" 7 "github.com/enetx/g/cmp" 8 ) 9 10 func main() { 11 // convert std slice of strings to g.Slice of g.String 12 strslice := []string{"aa", "bb", "cc"} 13 gs := g.SliceMap(strslice, g.NewString) // g.Slice[g.String] 14 15 gs.SortBy(func(a, b g.String) cmp.Ordering { return a.Cmp(b) }) 16 fmt.Println(gs) 17 18 // convert std slice of ints to g.Slice of g.Int 19 intslice := []int{1, 2, 3} 20 g.SliceMap(intslice, g.NewInt) // g.Slice[g.Int] 21 22 // convert std slice of floats to g.Slice of g.Float 23 floatslice := []float64{1.1, 2.2, 3.3} 24 g.SliceMap(floatslice, g.NewFloat) // g.Slice[g.Float] 25 26 //////////////////////////////////////////////////////////////////////////// 27 28 s := g.SliceOf[g.Int](1, 2, 3, 4, 5) 29 30 ss := g.SliceMap(s, g.Int.ToString) // g.Slice[g.String] 31 ss.Get(0).Format("hello %s").Print() 32 33 is := g.SliceMap(ss, func(s g.String) g.Int { return s.ToInt().Unwrap() }) // g.Slice[g.Int] 34 is.Get(0).Add(99).Print() 35 36 //////////////////////////////////////////////////////////////////////////// 37 38 ss1 := g.SliceOf[g.String]("1", "22", "3a", "44") 39 is1 := g.SliceMap(ss1, g.String.ToInt).Iter().Filter(g.Result[g.Int].IsOk).Collect() 40 41 g.SliceMap(is1, g.Result[g.Int].Ok).Print() // Slice[1, 22, 44] 42 }