Age input error

Report a typo

Suppose you want to create a very simple function checkAge() that takes as an argument the user's age and returns the following custom errors depending on the age:

  • "age can't be negative" if the user's age is less than 0;
  • "age can't be zero" if the user's age is equal to 0;
  • "babies and toddlers can't code" if the user's age is between 1 and 4 years.

Finally, if the user's age is greater than or equal to 5 years, your program should return nil and then print the message: "you're old enough to learn how to code in Go".

Sample Input 1:

-50

Sample Output 1:

age can't be negative

Sample Input 2:

5

Sample Output 2:

you're old enough to learn how to code in Go
Write a program in Go
package main

import (
"errors"
"fmt"
)

func checkAge(age int) error {
switch {
case age < ?:
return errors.New("age can't be negative")
case age == 0:
return ?
case age > ? && <= 4:
return ?
default:
return ?
}
}

// DO NOT delete or modify the contents of the main() function!
func main() {
var age int
fmt.Scanln(&age)

err := checkAge(age)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("you're old enough to learn how to code in Go")
}
}
___

Create a free account to access the full topic