From the course: Learning Go
Unlock this course with a free trial
Join today to access over 24,200 courses taught by industry experts.
Solution: Create a simple calculator app - Go Tutorial
From the course: Learning Go
Solution: Create a simple calculator app
(upbeat music) - [Instructor] In this challenge, you're given two values that are typed as strings. These values are passed into a function named calculate which returns a float64 value. I'm going to test my code and show that I've solved the challenge and that my code return to value of 15.5, which is the sum of the two values. Here's how I solved the challenge. First of all, I parsed the two values. I did this using the function ParseFloat, which is a member of the strconv or string conversion package. Notice that I'm using the TrimSpace function from the strings package to make sure that any extra space around the values is taken away. And then I'm also passing in a value of 64, meaning I'm getting back a float64, not a float32. Next, I check my error state. If the error isn't nil, that means there was an error. And in that case, I'm printing the error and then panicking, meaning I'm stopping the application…
Contents
-
-
-
-
Explore Go's variable types3m 16s
-
(Locked)
Declare and initialize variables5m 55s
-
(Locked)
Get input from the console3m 22s
-
(Locked)
Convert string inputs to other types4m 10s
-
(Locked)
Use math operators2m 3s
-
(Locked)
Use the math package5m 20s
-
(Locked)
Work with dates and times3m 50s
-
(Locked)
Solution: Create a simple calculator app2m 26s
-
-
-
-
-
-