Skip to content

Commit 789c21e

Browse files
committed
9-11 tasks done
1 parent 89c8677 commit 789c21e

22 files changed

Lines changed: 400 additions & 3 deletions

File tree

courses/golang/ex09-tcp-fibonacci/.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

courses/golang/ex09-tcp-fibonacci/.idea/ex09-tcp-fibonacci.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

courses/golang/ex09-tcp-fibonacci/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

courses/golang/ex09-tcp-fibonacci/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"encoding/json"
6+
"fmt"
7+
"net"
8+
"os"
9+
"strconv"
10+
)
11+
12+
func main() {
13+
conn, err := net.Dial("tcp", "127.0.0.1:8080")
14+
handleError(err)
15+
16+
defer conn.Close()
17+
18+
// var resp Response
19+
scan := bufio.NewScanner(os.Stdin)
20+
for scan.Scan() {
21+
input, err := strconv.ParseInt(scan.Text(), 10, 64)
22+
handleError(err)
23+
request := RequestFib{Number: int(input)}
24+
25+
encoder := json.NewEncoder(conn)
26+
handleError(encoder.Encode(request))
27+
28+
var resp ResponseFib
29+
decoder := json.NewDecoder(conn)
30+
handleError(decoder.Decode(&resp))
31+
32+
fmt.Printf("%s %d\n", resp.Time, resp.Fib)
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"time"
7+
)
8+
9+
type RequestFib struct {
10+
Number int
11+
}
12+
13+
type ResponseFib struct {
14+
Number int
15+
Fib *big.Int
16+
Time time.Duration
17+
}
18+
19+
var fibCache map[int]*big.Int
20+
21+
func fib(num int) *big.Int {
22+
if num < 0 {
23+
panic("num < 0")
24+
}
25+
26+
if val, ok := fibCache[num]; ok {
27+
return val
28+
}
29+
30+
size := len(fibCache)
31+
a := fibCache[size-2]
32+
b := fibCache[size-1]
33+
34+
for i := size; i <= num; i++ {
35+
a.Add(a, b)
36+
fibCache[i] = a
37+
a, b = b, a
38+
}
39+
return b
40+
}
41+
42+
func handleError(err error) {
43+
if err == nil {
44+
return
45+
}
46+
fmt.Println("Error: ", err.Error())
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module main
2+
3+
go 1.17
4+
5+
require github.com/stretchr/testify v1.7.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
11+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
8+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
13+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"math/big"
7+
"net"
8+
"time"
9+
)
10+
11+
12+
13+
func main() {
14+
fibCache = make(map[int]*big.Int)
15+
fibCache[0] = big.NewInt(0)
16+
fibCache[1] = big.NewInt(1)
17+
18+
fmt.Println("Booting up server...")
19+
const port string = "8080"
20+
21+
ln, err := net.Listen("tcp", ":"+port)
22+
handleError(err)
23+
fmt.Printf("Listening on localhost:%v\n", port)
24+
conn, err := ln.Accept()
25+
handleError(err)
26+
27+
for {
28+
var request RequestFib
29+
decoder := json.NewDecoder(conn)
30+
handleError(decoder.Decode(&request))
31+
32+
start := time.Now()
33+
resp := ResponseFib{
34+
Number: request.Number,
35+
Fib: fib(request.Number),
36+
Time: time.Since(start),
37+
}
38+
39+
enc := json.NewEncoder(conn)
40+
handleError(enc.Encode(resp))
41+
}
42+
}

courses/golang/ex10-workerpool/.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)