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,12 @@
|
||||
# 05 — Interfejsy i Kompozycja („OOP po Go-owemu”)
|
||||
|
||||
W Go **nie ma dziedziczenia klas** — jest **embedding** (kompozycja struktur) oraz **niejawne interfejsy** (typ spełnia interfejs automatycznie, jeśli ma wymagane metody).
|
||||
|
||||
## Zadania (`interfaces.go`)
|
||||
1. `EmailNotifier` i `SmsNotifier` spełniające interfejs `Notifier`.
|
||||
2. `AlertService.Broadcast(msg string) []string` — wywołuje `Format(msg)` dla każdego notifiera.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=./exercises/01-core/05-interfaces-composition
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
package interfaces
|
||||
|
||||
type Notifier interface {
|
||||
Format(msg string) string
|
||||
}
|
||||
|
||||
type EmailNotifier struct {
|
||||
Address string
|
||||
}
|
||||
|
||||
func (e EmailNotifier) Format(msg string) string {
|
||||
panic("TODO: zwróć '[email] ' + msg")
|
||||
}
|
||||
|
||||
type SmsNotifier struct {
|
||||
Phone string
|
||||
}
|
||||
|
||||
func (s SmsNotifier) Format(msg string) string {
|
||||
panic("TODO: zwróć '[sms] ' + msg")
|
||||
}
|
||||
|
||||
type AlertService struct {
|
||||
Notifiers []Notifier
|
||||
}
|
||||
|
||||
func (a *AlertService) Broadcast(msg string) []string {
|
||||
panic("TODO: wywołaj Format(msg) dla każdego notifiera i zwróć wyniki")
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAlertService(t *testing.T) {
|
||||
svc := AlertService{
|
||||
Notifiers: []Notifier{
|
||||
EmailNotifier{Address: "ops@example.com"},
|
||||
SmsNotifier{Phone: "+48123456789"},
|
||||
},
|
||||
}
|
||||
got := svc.Broadcast("Node down")
|
||||
want := []string{"[email] Node down", "[sms] Node down"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user