func main(){ p:=InMemSort(ArraySource(3,2,6,7,4)) for v := range p { fmt.Println(v) } } func ArraySource(a ...int) <-chan int{ out:=make(chan int) go func() { for _,v := range a{ out <-v } close(out) }() return out } func InMemSort(in <-chan int) <-chan int { out:=make(chan int) go func() { a:=[]int{} for v:= range in { a=append(a,v) } sort.Ints(a) for _,v:= range a{ out <-v } close(out) }() return out }
goroutine函数实现排序
最新推荐文章于 2023-06-01 21:56:14 发布