1. How QuickSort take in and out as arguments? Does it receive from the
line below?
This code pushes 100 random into into the channel called "in". You previously passed a reference to this channel to the quicksort function. This is the same idea as if I pass a function a thread-safe stack, and then from the callers context push a new element onto that stack.
for i := 0; i < 100; i++ {
in <- rand.Intn(1000)
}
close(in)
2. Is this case optimal using channel? It looks pretty neat receiving
values dynamically... What would be different just sorting without
channel? This case is faster?
I would consider this a cool toy example of how flexibly channels can be used (and a streaming sort). In most common cases, it is usually going to be much faster/easier to take a slice and call sort.Sort on it. Its also worth noting in most real world cases you will get better throughput by creating a channel with a buffer, as this will reduce the scheduler switching between goroutines. Channels are very fast, but they still have overhead and if you are not actually processing in parallel that overhead isn't buying you anything.
If you want to be processing in parallel don't forgot to set GOMAXPROCS > 1 and use buffered channels.