Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dgunjetti committed Aug 23, 2020
1 parent cb11eec commit c1cde97
Show file tree
Hide file tree
Showing 34 changed files with 1,061 additions and 2 deletions.
38 changes: 38 additions & 0 deletions 01-intro-solution/01-goroutines/01-hello/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"time"
)

func fun(s string) {
for i := 0; i < 3; i++ {
fmt.Println(s)
time.Sleep(1 * time.Millisecond)
}
}

func main() {
// Direct call
fun("direct call")

// write goroutine with different variants for function call.

// goroutine function call
go fun("goroutine-1")

// goroutine with anonymous function
go func() {
fun("goroutine-2")
}()

// goroutine with function value call
fv := fun
go fv("goroutine-3")

// wait for goroutines to end
fmt.Println("waiting for goroutines to complete..")
time.Sleep(1 * time.Second)

fmt.Println("done..")
}
29 changes: 29 additions & 0 deletions 01-intro-solution/01-goroutines/02-client-server/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"io"
"log"
"net"
"os"
)

func main() {
// connect to server on localhost port 8000
conn, err := net.Dial("tcp", "localhost:8000")

if err != nil {
log.Fatal(err)
}

defer conn.Close()

mustCopy(os.Stdout, conn)

}

// mustCopy - utility function
func mustCopy(dst io.Writer, src io.Reader) {
if _, err := io.Copy(dst, src); err != nil {
log.Fatal(err)
}
}
38 changes: 38 additions & 0 deletions 01-intro-solution/01-goroutines/02-client-server/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"io"
"log"
"net"
"time"
)

func main() {
// write server program to handle concurrent client connections.
listener, err := net.Listen("tcp", "localhost:8000")

if err != nil {
log.Fatal(err)
}

for {
conn, err := listener.Accept()

if err != nil {
continue
}
go handleConn(conn)
}
}

// handleConn - utility function
func handleConn(c net.Conn) {
defer c.Close()
for {
_, err := io.WriteString(c, "response from server\n")
if err != nil {
return
}
time.Sleep(time.Second)
}
}
23 changes: 23 additions & 0 deletions 01-intro-solution/02-join/01-join/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
"sync"
)

func main() {
// program to print the value as 1
// deterministically.
var wg sync.WaitGroup
var data int

wg.Add(1)
go func() {
defer wg.Done()
data++
}()
wg.Wait()
fmt.Printf("the value of data is %v\n", data)

fmt.Println("Done..")
}
26 changes: 26 additions & 0 deletions 01-intro-solution/02-join/02-join/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"runtime"
"sync"
)

// what will be output

func main() {
runtime.GOMAXPROCS(1)
var wg sync.WaitGroup

done := false

wg.Add(1)
go func() {
defer wg.Done()
done = true
}()
wg.Wait()
for !done {
}
fmt.Println("finished")
}
29 changes: 29 additions & 0 deletions 01-intro-solution/03-closure/01-closure/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"sync"
)

//TODO: run the program and check that variable i
// was pinned for access from goroutine even after
// enclosing function returns.

func main() {
var wg sync.WaitGroup

incr := func(wg *sync.WaitGroup) {
var i int
wg.Add(1)
go func() {
defer wg.Done()
i++
fmt.Println(i)
}()
return
}

incr(&wg)
wg.Wait()
fmt.Println("done..")
}
22 changes: 22 additions & 0 deletions 01-intro-solution/03-closure/02-closure/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"sync"
)

func main() {
var wg sync.WaitGroup

// what is the output
//TODO: fix the issue.

for i := 0; i < 3; i++ {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println(i)
}()
}
wg.Wait()
}
26 changes: 26 additions & 0 deletions 01-intro-solution/03-closure/03-closure/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"sync"
)

// What will be printed when the code below is executed?
// And fix the issue to assure that `len(m)` is printed as 10.

func main() {
m := make(map[int]int)

wg := &sync.WaitGroup{}
mu := &sync.Mutex{}
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
mu.Lock()
m[i] = i
mu.Unlock()
}()
}
wg.Wait()
println(len(m))
}
9 changes: 9 additions & 0 deletions 01-intro-solution/04-channel/01-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
go func(a, b int) {
c := a + b
}(1, 2)
// TODO: get the value computed from goroutine
// fmt.Printf("computed value %v\n", c)
}
12 changes: 12 additions & 0 deletions 01-intro-solution/04-channel/02-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {
go func() {
for i := 0; i < 6; i++ {
// TODO: send iterator over channel
}
}()

// TODO: range over channel to recv values

}
23 changes: 23 additions & 0 deletions 01-intro-solution/04-channel/03-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
)

func main() {
ch := make(chan int)

go func() {
defer close(ch)

// TODO: send all iterator values on channel without blocking
for i := 0; i < 6; i++ {
fmt.Printf("Sending: %d\n", i)
ch <- i
}
}()

for v := range ch {
fmt.Printf("Received: %v\n", v)
}
}
20 changes: 20 additions & 0 deletions 01-intro-solution/04-channel/04-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

// TODO: Implement ping pong with Channel Direction

func ping() {
// send message on ch1
}

func pong() {
// recv message on ch1
// send it on ch2
}

func main() {
// create ch1 and ch2

// spine goroutine ping and pong

// recv message on ch2
}
20 changes: 20 additions & 0 deletions 01-intro-solution/04-channel/05-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"

func main() {
//TODO: create channel owner goroutine which return channel and
// writes data into channel and
// closes the channel when done.

consumer := func(ch <-chan int) {
// read values from channel
for v := range ch {
fmt.Printf("Received: %d\n", v)
}
fmt.Println("Done receiving!")
}

ch := owner()
consumer(ch)
}
23 changes: 23 additions & 0 deletions 01-intro-solution/05-select/01-select/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"time"
)

func main() {
ch1 := make(chan string)
ch2 := make(chan string)

go func() {
time.Sleep(1 * time.Second)
ch1 <- "one"
}()

go func() {
time.Sleep(2 * time.Second)
ch2 <- "two"
}()

// TODO: multiplex recv on channel - ch1, ch2

}
20 changes: 20 additions & 0 deletions 01-intro-solution/05-select/02-select/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"time"
)

func main() {
ch := make(chan string, 1)

go func() {
time.Sleep(2 * time.Second)
ch <- "one"
}()

// TODO: implement timeout for recv on channel ch

m := <-ch
fmt.Println(m)
}
21 changes: 21 additions & 0 deletions 01-intro-solution/05-select/03-select/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"time"
)

func main() {
ch := make(chan string)

go func() {
time.Sleep(1 * time.Second)
ch <- "one"
}()

// TODO: if there is no value on channel, do not block.

m := <-ch
fmt.Println(m)

}
Loading

0 comments on commit c1cde97

Please sign in to comment.