Processing data
Data processing is when raw data is fed into a process, analyzed, and then used to generate useful information or output. At a very general level, this can include sorting data, searching or querying for data, and converting data from one type of input into another. For a CLI, the input can be received in the different ways discussed in the previous section. When receiving arguments using the Cobra framework, all the values are read in as string input. However, given a string of 123
, we can do a type check by utilizing the strconv
package’s Atoi
method, which converts an ASCII string into an integer:
val, err := strconv.Atoi("123")
If the string value cannot be converted because it isn’t a string representation of an integer, then an error will be thrown. If the string is a representation of an integer, then the integer value will be stored in the val
variable.
The strconv
package can be used to check, with conversion, many other types...