Skip to content

Commit

Permalink
Started adding channel examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
baudekin committed Feb 26, 2024
1 parent f966097 commit 02f308b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 06-channels-basic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
)

func main() {

// Show I gets get reference by the go routine
var ch = make(chan int)

go func(x int, y int) {
ch <- x + y
}(100, 200)

k := <-ch

fmt.Printf("Channel Sum: %d\n", k)

}
22 changes: 22 additions & 0 deletions 07-channel-iterator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
)

func main() {

var ch = make(chan int)

go func(cnt int) {
defer close(ch)
for i := 0; i < cnt; i++ {
ch <- i
}
}(10)

// process the received values
for i := range ch {
fmt.Println(i)
}
}
33 changes: 33 additions & 0 deletions 08-concurrent-channel-iterator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"
"sync"
)

var wg sync.WaitGroup

func main() {

var ch = make(chan int)

wg.Add(1)
go func(cnt int) {
defer close(ch)
for i := 0; i < cnt; i++ {
ch <- i
}
wg.Done()
}(10000)

wg.Add(1)
// Note the this causes deadlock. The question is why?
go func() {
// process the received values using select
for i := range ch {
fmt.Println(i)
}
wg.Done()
}()
wg.Wait()
}
36 changes: 36 additions & 0 deletions 09-non-blocking-concurrent-channel-iterator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"sync"
)

var wg sync.WaitGroup

func main() {
var cnt int = 10000

// Note we can
var ch = make(chan int, cnt)

wg.Add(1)
go func(cnt int) {
defer close(ch)
// Send everything without blocking
for i := 0; i < cnt; i++ {
ch <- i
}
wg.Done()
}(cnt)

wg.Add(1)
// Note the this causes deadlock. The question is why?
go func() {
// process the received values using select
for i := range ch {
fmt.Println(i)
}
wg.Done()
}()
wg.Wait()
}
47 changes: 47 additions & 0 deletions 10-concurrent-channel-selector/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"sync"
)

var wg sync.WaitGroup

func main() {

var ch = make(chan int, 0)

wg.Add(1)
go func(cnt int) {
defer close(ch)
for i := 0; i < cnt; i++ {
ch <- i
}
ch <- -1
wg.Done()
}(10)

wg.Add(1)
// Note the this causes deadlock. The question is why?
go func() {
defer wg.Done()
// process the received values using select
for {
select {
case i := <-ch:
if i < 0 {
fmt.Println("Exiting")
return
}
fmt.Println(i)
/*
case <-time.After(1 * time.Second):
fmt.Println("timeout")
return
*/
}

}
}()
wg.Wait()
}
19 changes: 19 additions & 0 deletions 11-race-condition-debugging/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import "fmt"

// This example comes from https://go.dev/doc/articles/race_detector
// go run -race main.go
func main() {
c := make(chan bool)
m := make(map[string]string)
go func() {
m["1"] = "a" // First conflicting access.
c <- true
}()
m["2"] = "b" // Second conflicting access.
<-c
for k, v := range m {
fmt.Println(k, v)
}
}

0 comments on commit 02f308b

Please sign in to comment.