github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2013/go1.1/chanof.go (about) 1 // +build OMIT 2 3 package main 4 5 import ( 6 "fmt" 7 "reflect" 8 ) 9 10 func sendSlice(slice interface{}) (channel interface{}) { 11 sliceValue := reflect.ValueOf(slice) 12 chanType := reflect.ChanOf(reflect.BothDir, sliceValue.Type().Elem()) 13 chanValue := reflect.MakeChan(chanType, 0) 14 go func() { 15 for i := 0; i < sliceValue.Len(); i++ { 16 chanValue.Send(sliceValue.Index(i)) 17 } 18 chanValue.Close() 19 }() 20 return chanValue.Interface() 21 } 22 23 func main() { 24 ch := sendSlice([]int{1, 2, 3, 4, 5}).(chan int) 25 for v := range ch { 26 fmt.Println(v) 27 } 28 }