package main import ( "bufio" "fmt" "io" "os" ) type Context struct { stdout io.Writer stderr io.Writer stdin io.Reader } type GetCommandResult int const ( GetCommandResultOk GetCommandResult = iota GetCommandResultError ) func getCommand(ctx *Context) (string, error) { fmt.Print("$ ") command_line, err := bufio.NewReader(ctx.stdin).ReadString('\n') if err != nil { return "", err } command, err := parseCommand(ctx, command_line) if err != nil { return "", err } return command, nil } func parseCommand(ctx *Context, command_line string) (string, error) { _ = ctx return command_line[:len(command_line)-1], nil } func handleCommand(ctx Context, command string) { fmt.Fprintln(ctx.stderr, command+": command not found") } func main() { ctx := Context{ stdout: os.Stdout, stderr: os.Stderr, stdin: os.Stdin, } for { command, err := getCommand(&ctx) if err != nil { fmt.Fprintln(os.Stderr, "Error reading input: ", err) os.Exit(1) } handleCommand(ctx, command) } }