chore: initialize containerized Go learning workbook (2026 DevOps/SRE)
This commit is contained in:
commit
84be3e5d77
64 files changed
+1382
No files matched your search
@@ -0,0 +1,10 @@
|
||||
# 09 — Wzorce Projektowe w Go (Functional Options, Worker Pool)
|
||||
|
||||
## Zadania (`patterns.go`)
|
||||
1. `NewClient(opts ...Option) Client` — wzorzec **Functional Options** (`WithTimeout`, `WithRetries`, domyślne wartości `5s` i `3`).
|
||||
2. `RunWorkerPool(workers int, jobs []int, fn func(int) int) []int` — przetwórz `jobs` przez `workers` goroutines i zwróć wyniki (w dowolnej lub tej samej kolejności — test sprawdza sumę wyników).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/01-core/09-design-patterns
|
||||
```
|
||||
@@ -0,0 +1,26 @@
|
||||
package patterns
|
||||
|
||||
import "time"
|
||||
|
||||
type Client struct {
|
||||
Timeout time.Duration
|
||||
Retries int
|
||||
}
|
||||
|
||||
type Option func(*Client)
|
||||
|
||||
func WithTimeout(d time.Duration) Option {
|
||||
panic("TODO: zwróć Option ustawiającą c.Timeout = d")
|
||||
}
|
||||
|
||||
func WithRetries(n int) Option {
|
||||
panic("TODO: zwróć Option ustawiającą c.Retries = n")
|
||||
}
|
||||
|
||||
func NewClient(opts ...Option) Client {
|
||||
panic("TODO: utwórz Client{Timeout: 5 * time.Second, Retries: 3} i zastosuj opts")
|
||||
}
|
||||
|
||||
func RunWorkerPool(workers int, jobs []int, fn func(int) int) []int {
|
||||
panic("TODO: uruchom workers goroutines pobierających zadania z kanału")
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package patterns
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFunctionalOptions(t *testing.T) {
|
||||
c := NewClient(WithTimeout(2*time.Second), WithRetries(5))
|
||||
if c.Timeout != 2*time.Second || c.Retries != 5 {
|
||||
t.Fatalf("unexpected client: %+v", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkerPool(t *testing.T) {
|
||||
results := RunWorkerPool(3, []int{1, 2, 3, 4, 5}, func(x int) int { return x * 10 })
|
||||
if len(results) != 5 {
|
||||
t.Fatalf("expected 5 results, got %d", len(results))
|
||||
}
|
||||
sum := 0
|
||||
for _, r := range results {
|
||||
sum += r
|
||||
}
|
||||
if sum != 150 {
|
||||
t.Fatalf("got sum %d, want 150", sum)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user