More Useful Functions
02/01/21 08:09 Filed in: GO
Last time I learned about using the time package, substring replacement, and getting user input. I learned functions can return more than one value, including an error. Today, I look into handling returned errors and the if statement.
When I used the command:
string, _ = reader.ReadString('\n')
fmt.Println(string)
to get user input from the keyboard, I used the blank _ operator to ignore the returned error code. If I want to actually do something with the error return I can use the log package to log fatal errors. But I only want to log a fatal error if I actually get an error. To determine this I can use GO's if statement. The syntax of the if statement is:
if condition {
// code
} else if condition 2 {
// code
…
} else {
// code
}
This is similar to a lot of other programming languages. Here is an example of using both to get the user's name from the keyboard.
This slightly modified version of the same program, lets me talk about another useful string function.
This program has a problem. The if statement on line 18 compares a string to a number. This won't work. I need to convert the string entered by the user and read in line 13 to a number. I also need to strip out the trailing newline that is sent when you hit the Enter/Return key on the keyboard. Most languages have a way to strip out white space from the ends of a string. GO is no different. It has a function, interestingly named, TrimSpace in the strings package that will do just that. I say interestingly named because it not only trims spaces from the ends of a string, but also the return newline character. The function returns a copy of the trimmed string. We use it like this:
input := string.TrimSpace(input)
This results in a string that we can now coerce into a number. GO has no built-in way to do this, so we need to use a different package: strconv, and the ParseFloat function. This function takes two arguments: the string to convert, and the number of bits of precision to use when converting. It is another function that returns an error code.
num, err := strconv.ParseFloat(input,64) // 64 bits of precision
Let's add these to the program above.
The new lines are 20-24. This is what happens when you run it with good input and bad input that fails on line 21.
This also shows what the log message looks like. In this case it shows the function that failed (ParseFloat), the input test, and the actual error, invalid syntax. The message returned isn't precise enough. It should probably say, "Converting string to number failed." Oh well.
I've been looking at various miscellaneous packages and functions that I tend to use a lot in any language in which I program. I should dive into more of the base language. That's for next time.
Logging Fatal Errors
When I used the command:
string, _ = reader.ReadString('\n')
fmt.Println(string)
to get user input from the keyboard, I used the blank _ operator to ignore the returned error code. If I want to actually do something with the error return I can use the log package to log fatal errors. But I only want to log a fatal error if I actually get an error. To determine this I can use GO's if statement. The syntax of the if statement is:
if condition {
// code
} else if condition 2 {
// code
…
} else {
// code
}
This is similar to a lot of other programming languages. Here is an example of using both to get the user's name from the keyboard.
Trimming Strings and Coercion
This slightly modified version of the same program, lets me talk about another useful string function.
This program has a problem. The if statement on line 18 compares a string to a number. This won't work. I need to convert the string entered by the user and read in line 13 to a number. I also need to strip out the trailing newline that is sent when you hit the Enter/Return key on the keyboard. Most languages have a way to strip out white space from the ends of a string. GO is no different. It has a function, interestingly named, TrimSpace in the strings package that will do just that. I say interestingly named because it not only trims spaces from the ends of a string, but also the return newline character. The function returns a copy of the trimmed string. We use it like this:
input := string.TrimSpace(input)
This results in a string that we can now coerce into a number. GO has no built-in way to do this, so we need to use a different package: strconv, and the ParseFloat function. This function takes two arguments: the string to convert, and the number of bits of precision to use when converting. It is another function that returns an error code.
num, err := strconv.ParseFloat(input,64) // 64 bits of precision
Let's add these to the program above.
The new lines are 20-24. This is what happens when you run it with good input and bad input that fails on line 21.
This also shows what the log message looks like. In this case it shows the function that failed (ParseFloat), the input test, and the actual error, invalid syntax. The message returned isn't precise enough. It should probably say, "Converting string to number failed." Oh well.
I've been looking at various miscellaneous packages and functions that I tend to use a lot in any language in which I program. I should dive into more of the base language. That's for next time.