Challenge 3 — Solution Task: Write a function SafeDivide(a, b float64) (float64, error) returning an error for division by zero. Write a table-driven test using t.Run for named subtests, covering a normal division and a divide-by-zero case, checking both the result/error appropriately for each. // divide.go package main import "errors" func SafeDivide(a, b float64) (float64, error) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil } // divide_test.go package main import "testing" func TestSafeDivide(t *testing.T) { tests := []struct { name string a, b float64 want float64 wantErr bool }{ {"normal division", 10, 2, 5, false}, {"divide by zero", 10, 0, 0, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result, err := SafeDivide(tt.a, tt.b) if tt.wantErr { if err == nil { t.Fatalf("expected an error, got none") } return } if err != nil { t.Fatalf("unexpected error: %v", err) } if result != tt.want { t.Errorf("SafeDivide(%v, %v) = %v; want %v", tt.a, tt.b, result, tt.want) } }) } } Expected result of running "go test -v": === RUN TestSafeDivide === RUN TestSafeDivide/normal_division --- PASS: TestSafeDivide/normal_division (0.00s) === RUN TestSafeDivide/divide_by_zero --- PASS: TestSafeDivide/divide_by_zero (0.00s) --- PASS: TestSafeDivide (0.00s) PASS Notes: - t.Fatalf is used for the "expected an error" check because if no error came back when one was expected, checking result against want next wouldn't make sense — the function clearly already misbehaved. - t.Run gives each case its own named line in verbose output (TestSafeDivide/normal_division, TestSafeDivide/divide_by_zero), making it immediately obvious which specific case failed if one does. - The wantErr field lets a single table and a single loop handle both "should succeed" and "should fail" cases cleanly, branching inside the subtest rather than needing two separate test functions.