Day 6: Error Handling & Validation 🤖
On Day 6, we conquer Go error handling, data conversion, and user input validation to make our CLI even more awesome! 🌟
Welcome back, coding pals! 🥳 On Day 6, we're diving into the world of Go errors and error handling. We'll learn how to improve our CLI by accepting and validating user input. Let's get started!
🚧 Introduction to Go Errors and Error Handling 🚧
Errors in Go are values. They indicate that something unexpected has occurred. Go has a built-in error interface that looks like this:
An interface in Go is like a set of rules that an object should follow. It outlines the methods the object must have, but doesn't provide any actual code for those methods. In the case of the error
interface, it means that any type implementing the Error() string
method can be treated as an error.
To handle errors in Go, we usually use a multiple return pattern. Functions that might return an error will return an error value as their last return value. It's very common for functions and standard library packages to always return an error.
The most idiomatic and common way to handle errors in Go is to add an if err != nil
check after calling a function that returns an error.
Let's take a look at an example of creating and returning an error:
In the example above, we have a function validateAge
that returns an error if the input age is invalid.
🔢 Converting Strings to Numbers with strconv 🔢
Now let's say we want a CLI to accept numbers instead of just strings. We can use the strconv package from Go's standard library to convert strings to numbers. Let's create an example with CLI input:
In this example, we use strconv.Atoi
to convert the input string to an integer. If the conversion is successful, the program prints the number.
Create a new replit and play around with. Here’s mine 👉 https://replit.com/@periclestheo/15daysofgo-error-hander
🔐 Validating User Input and Improving CLI 🔐
Building on our course’s CLI, we'll now allow users to input a string between 8 and 45 characters. We'll handle any errors and return nice error messages to the user.
To do this, we can use the len()
function in Go. The len()
function returns the length of a given string. In our case, we'll use it to determine the length of the user's input string.
Here's an example of how we can validate the input length:
This code snippet checks if the input string is between 8 and 45 characters long. If not, it prints an error message and returns.
Keep in mind the version above could have also been implemented by just a boolean from validateInput
🙈.
🔗 Here’s my version. Update your CLI replit with the above code. Feel free to play around with whatever validation numbers you prefer.
🎉 Woohoo! Day 6, Done and Dusted! 🎉
Kudos, you've just leveled up your Go skills by learning about errors and validations! 🌟 You're starting to get a real grasp of the language now, and your CLI is coming together nicely. Who knew error handling could be so satisfying, right? 😉
Keep up the good work and remember, practice makes perfect! Next up, we'll dive into APIs, and trust me, it's gonna be a blast! 💥