From the course: Learning Go

Convert string inputs to other types - Go Tutorial

From the course: Learning Go

Convert string inputs to other types

- [Instructor] I've previously shown how to get input from the console using the bufio packages new reader function and a reader object and the OS.standard in object to get data specifically from what the user types in. These values always come to you as strings. But you may need to convert them to other types. So next, I'll show you how to take that string and convert it to another type. To do this, I'll need another package that's a part of the GoCo library called strconv for string conversion. And I'm also going to need a package named strings. This package contains all sorts of functions for manipulating strings. Now in my code, I'm going to copy this line. I'll paste it down here, and I'll change from enter text to enter a number. Then I'll create a variable that I'll call numInput. And once again, I'll be getting back an error object that I can ignore. So I'll name it with the underscore character. I'll use the colon equals operator again for the assignment, and then I'll call reader.ReadString. And once again, I'll parse in the line feed as the delimiter. Now I'm going to convert that string to a number. I'll create a variable called aFloat. And this time, I do care about the error object because it's really easy for the user to type in a string that can't be parsed into a number. I'll call strconv, that's the package, and then a function called ParseFloat. This function requires two parameters. First is the string that I'm going to convert. Now I could just parse in that num input variable. But if the user typed in space characters at the beginning or the end of the string, it won't parse correctly. So I'm going to use a function from the string package called TrimSpace. And that removes any leading or trailing whitespace. And I'll parse in numInput for that. And then the second value is the bit size. Parse in 32 if you want to float 32 value or 64 for float 64. I'm working on a 64 bit operating system. So I'll parse in 64. Now I need to evaluate that error object. The error will be nil if everything went fine. Or if there was a parsing problem, it'll come back as a non nil object. So I'll use a little bit of conditional code. I'll say if err doesn't equal nil, then I'll put in a pair of braces. And if that's the condition, then I'll say fmt.Printline, and I'll pass in the error object. And I'll see the actual error message that way. Then I'll put in an else section. And in this section, I'll assume that I have a string that the user typed in. And I once again call print line, and parse in value of number, and then the floating number aFloat. I'll save those changes. Then I'll go back to my explorer window, right click on the file and open it in the integrated terminal. List the contents of the directory to make sure I'm in the right place, then call go run dot. The first prompt is for any text I like. I'll type in any text and I see it repeated. And now I'm asked for a numeric value. And because I'm looking for a floating number, I can parse in a value with fractions. I'll pass in 42.2. And I get back the parsed number. Now I'll run the application again. This can be anything. And this time, I'll parse in a value that can't be parsed as number and I get back the error parsing xyz, invalid syntax. So that's how you can convert user input from the console into whatever type you like. The strconv package has functions for converting from a string. The string conversion package has functions that you can use to convert strings to many other go types.

Contents