Wrapped errors tell the whole story while still being easy to test.
What to build
A program that runs a list of stack commands and reports every error with its line number.
Requirements
- Read n, then n command lines (lines are numbered 1 to n):
push X,pop,dup(copy the top),addandmul(use the top two values and push the result). popanddupneed one value,addandmulneed two; otherwise the error isstack empty. Any other command, and apushwithout a valid whole number, gives the errorunknown command.- When a command fails, print
line N: MESSAGEwith N the line number, and leave the stack as it was. - At the end print
empty errors: A, unknown errors: B(use errors.Is to count them), and thenstack: [...]with the items from the bottom to the top.
Input
Line 1: n. Then n command lines.
Output
Error lines, then two summary lines.
Constraints
1 ≤ n ≤ 50; numbers are between -100 and 100.
Example
Input:
6
push 3
push 4
add
mul
dup
jump
Output:
line 4: stack empty
line 6: unknown command
empty errors: 1, unknown errors: 1
stack: [7 7]
Why it works
Each failure is a sentinel error wrapped with the line number by %w. Because the original error is still inside, errors.Is can count each kind at the end.
Examples
Example 1 · Input
6 push 3 push 4 add mul dup jump
Expected output
line 4: stack empty line 6: unknown command empty errors: 1, unknown errors: 1 stack: [7 7]
Example 2 · Input
4 pop push 5 dup mul
Expected output
line 1: stack empty empty errors: 1, unknown errors: 0 stack: [25]
Hints
Hint 1
Declare var ErrEmpty = errors.New("stack empty") and var ErrUnknown = errors.New("unknown command"), and wrap them with fmt.Errorf("line %d: %w", i, err).
Hint 2
Count the errors with errors.Is at the end.
Solution
Show one correct solution (try it yourself first)
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
var ErrEmpty = errors.New("stack empty")
var ErrUnknown = errors.New("unknown command")
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
stack := []int{}
empty, unknown := 0, 0
line := 0
for scanner.Scan() {
line++
f := strings.Fields(scanner.Text())
var err error
if len(f) == 0 {
err = ErrUnknown
} else {
switch f[0] {
case "push":
if len(f) < 2 {
err = ErrUnknown
} else if x, e := strconv.Atoi(f[1]); e != nil {
err = ErrUnknown
} else {
stack = append(stack, x)
}
case "pop":
if len(stack) < 1 {
err = ErrEmpty
} else {
stack = stack[:len(stack)-1]
}
case "dup":
if len(stack) < 1 {
err = ErrEmpty
} else {
stack = append(stack, stack[len(stack)-1])
}
case "add", "mul":
if len(stack) < 2 {
err = ErrEmpty
} else {
a, b := stack[len(stack)-2], stack[len(stack)-1]
stack = stack[:len(stack)-2]
if f[0] == "add" {
stack = append(stack, a+b)
} else {
stack = append(stack, a*b)
}
}
default:
err = ErrUnknown
}
}
if err != nil {
wrapped := fmt.Errorf("line %d: %w", line, err)
fmt.Println(wrapped)
if errors.Is(wrapped, ErrEmpty) {
empty++
}
if errors.Is(wrapped, ErrUnknown) {
unknown++
}
}
}
fmt.Printf("empty errors: %d, unknown errors: %d\n", empty, unknown)
fmt.Println("stack:", stack)
}