github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/enum.go (about) 1 package f 2 3 // Times returns a slice of n 0-sized elements, suitable for ranging over. 4 // 5 // For example: 6 // 7 // for i := range Times(10) { 8 // fmt.Println(i) 9 // } 10 // Times(10, func(i int) { 11 // fmt.Print(i) 12 // }) 13 // 14 // ... will print 0 to 9, inclusive. 15 // 16 // It does not cause any allocations. 17 func Times(n int, fn ...func(i int)) (s []struct{}) { 18 s = make([]struct{}, n) 19 for _, f := range fn { 20 for i := range s { 21 f(i) 22 } 23 } 24 return 25 } 26 27 // TimesRepeat create times. 28 func TimesRepeat(times int, value interface{}) []interface{} { 29 q := make([]interface{}, times) 30 for i := range q { 31 q[i] = value 32 } 33 return q 34 } 35 36 // TimesRepeatAppend append times. 37 func TimesRepeatAppend(slice []interface{}, times int, value interface{}) { 38 if slice == nil { 39 slice = make([]interface{}, 0, times) 40 } 41 q := TimesRepeat(times, value) 42 slice = append(slice, q...) 43 return 44 }