Returning errors
📖 Scenario: You are writing a simple program to check if a number is positive. If the number is not positive, you want to return an error message.
🎯 Goal: Build a Go program that defines a function to check if a number is positive and returns an error if it is not. Then call this function and handle the error properly.
📋 What You'll Learn
Create a function that returns an error when the input number is not positive
Use the built-in
errors.New function to create the errorCall the function with a negative number to test error handling
Print the error message if an error is returned
💡 Why This Matters
🌍 Real World
Returning errors is important in real programs to handle unexpected situations like invalid input or failed operations.
💼 Career
Understanding how to return and handle errors is a key skill for Go developers working on reliable and maintainable software.
Progress0 / 4 steps