Youtube comments of Kantan Coding (@kantancoding).
-
930
-
126
-
98
-
86
-
60
-
53
-
42
-
34
-
30
-
28
-
24
-
22
-
22
-
22
-
21
-
20
-
Hey thanks for the feedback! Actually, closing the channel isn't incorrect. In go, when you close the channel, the zero value for the channel's data type will get put onto the channel. So in this case, since the channel's data type is bool, when we close the channel in main, the message false gets put onto the channel.
The for select loop in doWork will then receive that value and it will trigger the return. That's because "case <-done" means that if ANY value true or false is received from the channel, that cases code will be executed, which in this case is a return.
The reason you can't see "fmt.Println("work done")" being printed in your code is because I actually forgot to add another sleep at the end of the main function to wait for the channel to close and for the code in that case block to be exectued lol.
Here, try this code and you'll see what I mean (would be cool if youtube would add code formatting to comments):
```
package main
import "fmt"
import "time"
func doWork(done <-chan bool) {
for {
select {
case something := <-done:
fmt.Println("From done channel:", something)
return
default:
fmt.Println("...Doing work")
}
}
}
func main() {
done := make(chan bool)
go doWork(done)
time.Sleep(time.Second *2)
close(done)
time.Sleep(time.Second *3)
}
```
P.S. I really appreciate your kind words and words of encouragement 😄
19
-
19
-
17
-
16
-
14
-
13
-
13
-
13
-
12
-
12
-
12
-
11
-
11
-
10
-
10
-
10
-
10
-
10
-
10
-
10
-
9
-
9
-
9
-
8
-
8
-
8
-
8
-
8
-
8
-
7
-
7
-
Yes, you are absolutely correct my friend. Good insight 😉
Basically, this video is to teach the orDone pattern. The context package needs its own entirely separate video (which I am currently making). I did not want to convolute the explanation of the orDone pattern with context package stuff.
But! for those that want to jump ahead, this is what the code looks like replacing the done channel with the context package(the pattern does not change):
package main
import (
"context"
"fmt"
"sync"
)
var wg sync.WaitGroup
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cows := make(chan interface{}, 100)
go func() {
for {
select {
case <-ctx.Done():
return
case cows <- "moo":
}
}
}()
wg.Add(1)
go consumeCows(ctx, cows)
wg.Wait()
}
func consumeCows(ctx context.Context, cows <-chan interface{}) {
defer wg.Done()
for val := range orDone(ctx, cows) {
fmt.Println(val)
}
}
func orDone(ctx context.Context, c <-chan interface{}) <-chan interface{} {
relayStream := make(chan interface{})
go func() {
defer close(relayStream)
for {
select {
case <-ctx.Done():
return
case v, ok := <-c:
if ok == false {
return
}
select {
case relayStream <- v:
case <-ctx.Done():
return
}
}
}
}()
return relayStream
}
7
-
7
-
7
-
7
-
7
-
6
-
6
-
6
-
6
-
6
-
6
-
6
-
6
-
6
-
6
-
6
-
6
-
6
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
5
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
4
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
3
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2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
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
2
-
1
-
1
-
1
-
1
-
1
-
1
-
Yeah, In CS we usually use base 2 as opposed to something like base 10 which is commonly used in mathematics. So if the base isn’t explicitly written… you can assume base 2 in CS. Just think about how memory and stuff like that is done in computers. It usually increments by 2 to some power. 2^1, 2^2, 2^3, 2^4, 2^5, 2^6 which is 2, 4, 8, 16, 32, 64… respectively. When you buy an SD card, you don’t buy a 60gig card.. you buy a 64 gig card. When you buy a new laptop, you don’t upgrade to 10 gigs of ram, you upgrade to 16 gigs of ram. Hope that helps! 😊
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
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1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
Hello sir. You will find that when it comes to DSA, there is really no difference between languages. For example, here is binary search in Go:
package main
import "fmt"
func main() {
fmt.Println(binarySearch([]int{-1, 0, 3, 5, 9, 12}, 3))
}
func binarySearch(nums []int, target int) int {
start := 0
end := len(nums) - 1
for start <= end {
mid := (start + end) / 2
if nums[mid] == target {
return mid
}
if nums[mid] < target {
start = mid + 1
}
if nums[mid] > target {
end = mid - 1
}
}
return -1
}
And here is binary search in Python:
def binarySearch(nums, target):
start = 0
end = len(nums) - 1
while start <= end:
mid = (start + end) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
start = mid + 1
if nums[mid] > target:
end = mid - 1
return -1
if _name_ == "__main__":
print(binarySearch([-1, 0, 3, 5, 9, 12], 3))
As you can see, the algorithms are exactly the same. The only difference is language syntax. You will also notice that if you can read one, you can read the other. So when learning dsa, don't worry so much about the language being used. If you find materials, that are helpful that use a different language from the language you're most comfortable with, odds are you can still understand the material if the teacher is doing what he/she is supposed to be doing.
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-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