More Useful Functions

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.


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.

Screen Shot 2021-02-01 at 8.30.57 AM

Screen Shot 2021-02-01 at 8.30.44 AM




Trimming Strings and Coercion



This slightly modified version of the same program, lets me talk about another useful string function.

Screen Shot 2021-02-01 at 8.42.31 AM


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.


Screen Shot 2021-02-01 at 9.00.37 AM


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.


Screen Shot 2021-02-01 at 9.00.22 AM

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.


This site does not track your information.