Check out a free preview of the full Basics of Go course
The "Calculator Project" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:
Maximiliano guides you through the process of creating a basic calculator in Go, which takes input from the standard input using Scanf. Additionally, different verb types for the fmt package functions are explained in this segment.
Transcript from the "Calculator Project" Lesson
[00:00:00]
>> Before talking about type definitions we need to make some quick examples working with the console. So I'm gonna create a new project, so then we remember again, how that works. So what is that creating new for folder first, I'm going to open a folder, and that was input output.
[00:00:17]
We can start now, for example, let's create a simple calculator. So I'm going to create a new folder for calculator, okay? So we're going to create the calculator. There we are. You wanna save functions.go, okay, save. So, first step is to open a terminal and we integrate the module.
[00:00:37]
Does anyone remember how to create the module? Go mod, init, and then the name typically starting with the URL. I can say frontendmasters.com/go/calc, any name we work, okay? And these, remember, the only thing that we are doing here is creating a new file, in the folder, in the root of our project with the name go.mod.
[00:01:04]
That's the only thing we have here. And typically, we need also at least one file that we can call it main.go. That we have package main, function, main. That's the boilerplate for every go project, okay? So far we've been dealing with using the format package that is a built-in package.
[00:01:36]
It's a system library, standard library. It's a standard library for sending outputs to the console, but also we can read values from the console. That is something similar on CLIs, Common Line Interfaces. And you can do common line interfaces because we are doing a scripting for the operating system.
[00:01:59]
So for example, we can, create the variable for an operation. We are going to create this really small kind, just to play with the console, it has nothing really fancy. And we're going to create two variables, number 1 or N1, whatever number 1, and number 2. If both variables are of the same type, you can use the type declaration 1s.
[00:02:22]
So you don't need to say int, int, like so. I mean, it's exactly the same. Here are two lines. You can use one line with a comma. And because they both have the same end on the same int, we can just use int add the last one only shortcuts, okay?
[00:02:41]
Why it's complaining? I'm not using the variables yet, which is okay. So this is our calculator. So we're going to start talking to our format library. So I will start typing fmt, fmt, return, and it should add the import for us at the top. I'm going to start print the line, something like calculator 1.0.
[00:03:10]
Like a title, and then we do some ASCII art, right? And some things like this. It would probably be horrible, but anyway. And the idea is that maybe we're gonna start asking we have another print line for the operation. But which operation do you want to perform? And we can say, you can say add, subtract, multiply, or divide.
[00:03:40]
Let me do this so it fits in the screen. So when the user to type in the console, add, subtract, it can also be 1, 2 or 3, it doesn't matter. So how to read a value from the console, we're also going to use font or format. And we're going to call scanf.
[00:04:02]
Scanf, we'll read text from the standard input. The standard input here is the console, the keyboard. But you can change that because you can actually run a Go application and select the input and the output at different buffers. It can be the network, in this case, it's the console.
[00:04:21]
So I'm going to just scanf. And scanf receives first a format, and the format is similar to printf. So if we're expecting an integer, we use %i. If we're expecting a float, f. If we're expecting a string, s. There is a table in the documentation on all the possible elements that you can use.
[00:04:45]
In fact, let me go to the website and show you where that is. So we go to go.dev. Docs, we should find it in standard library because this is an external library. We look here for format, fmt. And in the overview, directly in the overview, when you get into the documentation, you have all the verbs.
[00:05:11]
So v is a value, so we try to find the value, and here you have all the options. Booleans for integers, for floating points, based on what do you want to output or what do you want as an input. In fact, later we will use some of these more complicated formatting, such as what if you want to float with only two decimal values.
[00:05:34]
Well, playing with the percentage variable, we can change that. We can specify how we want to format a value, okay? So going back here, so we want scanf, and the next thing is where do you want to save the value. So we have a variable for that, it's called operation.
[00:05:56]
Now, in theory, this should read the operation., in theory, why? Scan is a function, right? It's a function, how do I know because I have parentheses executing, it's a function. And passing a variable as an argument, what happens with that argument? It's been copied. It's been passed by value.
[00:06:21]
So if I want the scanf to change the contents of my variable, I must send the address of my variable, &%. Does it make sense? Because if not, it's not gonna work. If you're expecting a function to change, to modify your variables, you always need to send the phone number of the variable.
[00:06:45]
So then that function can call the variable. Don't create a copy of it. So then I can do a switch and then ask, okay, let's see what do we have for operation, okay? And based on that, we do one thing or the other. But before doing that, I can also ask for the two numbers.
[00:07:06]
So it's pretty simple just to play with this and see if that works. So enter first number, and we are going to do another scanf for a number. And in where we are doing the charts, I can do floats as well, and save this as &%1, remember? &% because I want that function to modify this number 2, there we are.
[00:07:36]
And then it's just a couple of case. So if it's ad, we're going to print line or return. In this case, we are going to print line number 1 plus number 2 because I'm adding those numbers. When I need to break now, I will quickly do this subtract, multiply.
[00:08:03]
I got great constants for these strings, that's probably a good idea. Minus, multiply, division. That should work. And doing everything in the main function, that's typically not the best of the ideas, but we are playing with something different here. With the format, package and also with now that we know what the pointer is, we understand why we need to pass &%, the variable.
[00:08:36]
So it's like passing the empty box and said hey, fill me the box, okay? Put me the number in the box or put me the string in the box, this is the empty box. That's why I'm not defining an initial value for operations, I don't need to. I don't need to set a value.
[00:08:54]
So we have %s for strings, and we have an i here. If we go to the documentation that we have here for the format, and we search for numbers. Yeah, you can also send pointers if you wanna see a pointer in a different place how to send slides.
[00:09:14]
And if I wanna receive integers, it depends on which base do you want. I doesn't contain a base, typically we write numbers in base 10, that's the normal numbers, decimal numbers. So we can use D instead of I, so %D, like so. And then we can run this and say when a multiply 4 by 3 and the answer is 12.
[00:09:44]
So it seems like it's working, right? It's just that, not the big deal, not the big app. But we have been dealing with pointers here. And now we understand how that works and how to receive values from the scanner. The scanner scans the input. Again, the input can be something different than the keyboard.
[00:10:03]
By default, the keyboard, but for example you can pipe this in some operating systems, so instead of the keyword it can be the output from another app. So you can pipe the output of one app as an input of the next application.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops