validtn

command
v0.0.0-...-8c2c48f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 3 Imported by: 0

README

This program is like a number detective. It looks at a list of things you give it and tries to figure out which ones are whole numbers (like 1, 2, 3), which ones are numbers with decimal points (like 1.5, 2.7), and which ones aren't numbers at all. Then it tells you how many of each it found!

Now, let's go through the thought process of writing this code:

  1. We want to create a program that can analyze a list of inputs and categorize them as integers, floats, or invalid.

  2. First, we need to get the inputs from the user. In Go, we can use os.Args for this.

  3. We should check if the user gave us any inputs at all. If not, we'll tell them we need more.

  4. We'll need to keep track of how many integers, floats, and total valid numbers we find.

  5. We also want to keep a list of any inputs that aren't numbers.

  6. We'll look at each input one by one:

    • First, we'll try to turn it into an integer.
    • If that doesn't work, we'll try to turn it into a float.
    • If both of these fail, we'll call it invalid.
  7. After checking all inputs, we'll report our findings.

  8. If there are a lot of invalid inputs, we'll list them out.

Here's a breakdown of the code:

  1. We check if there are any command-line arguments. If not, we tell the user and exit.

  2. We set up variables to count total valid numbers, integers, and floats.

  3. We create a slice to store invalid inputs.

  4. We loop through each input (skipping the first one, which is the program name):

    • We try to convert it to an integer using strconv.Atoi.
    • If that works, we increase our counts and move to the next input.
    • If not, we try to convert it to a float using strconv.ParseFloat.
    • If that works, we increase our counts and move to the next input.
    • If both fail, we add the input to our list of invalid inputs.
  5. We print out how many total valid numbers, integers, and floats we found.

  6. If we have more invalid inputs than valid ones, we print out how many invalid inputs we found and list them.

This code demonstrates how to parse and categorize input data, handle different types of numbers, and keep track of invalid inputs. It's a useful approach for data validation and analysis tasks. The thought process involves breaking down the problem into steps: getting input, checking each input, categorizing it, and then reporting the results.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL