Complete the code to return an error from the function.
func check(value int) error {
if value < 0 {
return [1]("negative value")
}
return nil
}fmt.Println instead of returning an error.panic which stops the program instead of returning an error.The errors.New function creates a new error with the given message, which can be returned.
Complete the code to declare the function returns an error.
func divide(a, b int) (int, [1]) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil }
string instead of error.The function returns an int and an error type to indicate success or failure.
Fix the error in the code to properly return an error.
func openFile(name string) error {
f, err := os.Open(name)
if err != nil {
return [1]
}
defer f.Close()
return nil
}f instead of the error.nil when there is an error.When os.Open returns an error, it should be returned directly as err.
Fill both blanks to create and return a formatted error message.
func validate(age int) error {
if age < 18 {
return fmt.Errorf([1], [2])
}
return nil
}fmt.Errorf formats the error message with the age value.
Fill all three blanks to check error and handle it properly.
result, err := divide(10, 0) if [1] != nil { fmt.Println([2]) } else { fmt.Println([3]) }
Check if err is not nil to handle errors, otherwise print the result.
