Basics of Go

Calculator Project

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

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

>> 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.

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.

Does anyone remember how to create the module? Go mod, init, and then the name typically starting with the URL. I can say, 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.

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.

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.

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.

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?

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.

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.

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.

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.

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.

In fact, let me go to the website and show you where that is. So we go to 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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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
Get Unlimited Access Now