Building a simple command-line calculator with Go 👨‍💻
Tags: commandline, go, pr, building
Published: 2023-01-21 09:32:21

Building a simple command line calculator using the Go programming language is a great way to learn the basics of the language and gain experience with its built-in functionality. In this post, we will walk through the steps of creating a basic calculator that can perform addition, subtraction, multiplication, and division.

##Step 1: Set up the Go environment

The first step in building our calculator is to set up the Go environment on your computer. This includes installing the Go software and setting up the Go workspace, which is the directory where your Go code will be stored. If you haven't already, you can download the Go software from the official website (https://golang.org/dl/) and follow the installation instructions for your operating system.

##Step 2: Create a new project

Once the Go environment is set up, we can create a new project for our calculator. In the terminal, navigate to the Go workspace directory and create a new directory for our project. We can name it "calculator".

```
$ mkdir calculator
$ cd calculator

```
##Step 3: Write the code

Now that we have our project set up, we can start writing the code for our calculator. In the "calculator" directory, create a new file named "main.go". This is where we will write the main function for our program.

We will start by importing the fmt package which provides input/output operations.

```
package main
import "fmt"

```

Next, we will define the main function. Inside the main function, we will use a switch statement to handle the different operations (addition, subtraction, multiplication, and division).

```
func main() {
    var operator string
    fmt.Println("Enter operator: +, -, *, /")
    fmt.Scan(&operator)

    var num1, num2 float64
    fmt.Println("Enter two numbers:")
    fmt.Scan(&num1, &num2)

    switch operator {
    case "+":
        fmt.Println(num1 + num2)
    case "-":
        fmt.Println(num1 - num2)
    case "*":
        fmt.Println(num1 * num2)
    case "/":
        fmt.Println(num1 / num2)
    default:
        fmt.Println("Invalid operator")
    }
}

```

In this code, we first ask the user to enter an operator, and then two numbers. We then use the switch statement to check the value of the operator and perform the corresponding operation. If the operator entered is not one of the four supported operators, we print an error message.

##Step 4: Build and run the program

Once the code is written, we can build and run the program. In the terminal, navigate to the "calculator" directory and run the following command:

```
$ go run main.go

```

This will create an executable file in the "calculator" directory. To run the program, simply type the name of the executable file (in this case, "calculator") and press enter. The program will prompt you to enter an operator and two numbers, and then display the result of the calculation.

That's it! We have successfully built a simple command line calculator using the Go programming language. This calculator can be enhanced with more functionality, like handling more complex mathematical operations, handling floating point numbers and so on. 

👨‍💻codeDaily