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 @@
|
||||
# 08 — Struktury Danych (`Stack[T]`, `SafeCache[K, V]`)
|
||||
|
||||
## Zadania (`ds.go`)
|
||||
1. `Stack[T]` — generyczny stos (`Push`, `Pop`, `Len`).
|
||||
2. `SafeCache[K comparable, V any]` — thread-safe cache z `sync.RWMutex` (`Set`, `Get`, `Delete`).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/01-core/08-data-structures
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
package ds
|
||||
|
||||
import "sync"
|
||||
|
||||
type Stack[T any] struct {
|
||||
items []T
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Push(v T) {
|
||||
panic("TODO: dodaj v na wierzch stosu")
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Pop() (T, bool) {
|
||||
panic("TODO: zdejmij element lub zwróć (zero, false)")
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Len() int {
|
||||
return len(s.items)
|
||||
}
|
||||
|
||||
type SafeCache[K comparable, V any] struct {
|
||||
//lint:ignore U1000 używane po zaimplementowaniu metod
|
||||
mu sync.RWMutex
|
||||
data map[K]V
|
||||
}
|
||||
|
||||
func NewSafeCache[K comparable, V any]() *SafeCache[K, V] {
|
||||
return &SafeCache[K, V]{data: make(map[K]V)}
|
||||
}
|
||||
|
||||
func (c *SafeCache[K, V]) Set(k K, v V) {
|
||||
panic("TODO: użyj c.mu.Lock() + defer c.mu.Unlock()")
|
||||
}
|
||||
|
||||
func (c *SafeCache[K, V]) Get(k K) (V, bool) {
|
||||
panic("TODO: użyj c.mu.RLock() + defer c.mu.RUnlock()")
|
||||
}
|
||||
|
||||
func (c *SafeCache[K, V]) Delete(k K) {
|
||||
panic("TODO: usuń klucz pod c.mu.Lock()")
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package ds
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStack(t *testing.T) {
|
||||
var s Stack[int]
|
||||
s.Push(10)
|
||||
s.Push(20)
|
||||
if v, ok := s.Pop(); !ok || v != 20 {
|
||||
t.Fatalf("got (%d, %v), want (20, true)", v, ok)
|
||||
}
|
||||
if v, ok := s.Pop(); !ok || v != 10 {
|
||||
t.Fatalf("got (%d, %v), want (10, true)", v, ok)
|
||||
}
|
||||
if _, ok := s.Pop(); ok {
|
||||
t.Fatal("expected empty stack")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeCacheConcurrent(t *testing.T) {
|
||||
cache := NewSafeCache[int, int]()
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 20; i++ {
|
||||
wg.Add(1)
|
||||
go func(n int) {
|
||||
defer wg.Done()
|
||||
cache.Set(n, n*2)
|
||||
_, _ = cache.Get(n)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
if v, ok := cache.Get(5); !ok || v != 10 {
|
||||
t.Fatalf("got (%d, %v), want (10, true)", v, ok)
|
||||
}
|
||||
cache.Delete(5)
|
||||
if _, ok := cache.Get(5); ok {
|
||||
t.Fatal("expected key 5 deleted")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user