Golang: Calculator program

Akarsh Seggemu, M.Sc.
5 min readApr 1, 2024

--

Using Golang to create a calculator program

How to create a calculator program?

Calculator program requires performing four mathematical operations on two given inputs. The result is the output post execution of the mathematical operations. One crucial when programming is that we need to convert the input value into float data type as in programming we cannot do mathematical operation on different data types.

I have come up with the following solution in an algorithmic expression.

Step 1: Start
Step 2: Define the main function.
Step 2.1: Declare variables value1, value2, and operation, and
assign string values "10", "5.5", and "+" respectively.
Step 2.2: Call the function calculate with value1, value2, and
operation as inputs, storing the result in a variable
named result.
Step 2.3: Print "Result:" followed by the value of result.
Step 3: Define the function calculate, taking input1, input2, and operation
as strings, and returning a float64.
Step 3.1: Call the function convertInputToValue with input1 and
input2 as inputs, storing the results in floatInput1
and floatInput2 respectively.
Step 3.2: Declare a variable result of type float64.
Step 3.3: Use a switch statement on the operation.
Step 3.3.1: If operation is "+", assign the result of
adding floatInput1 and floatInput2 to result.
Step 3.3.2: If operation is "-", assign the result of
subtracting floatInput2 from floatInput1 to
result.
Step 3.3.3: If operation is "*", assign the result of
multiplying floatInput1 and floatInput2 to result.
Step 3.3.4: If operation is "/", assign the result of dividing
floatInput1 by floatInput2 to result.
Step 3.4: Return result.
Step 4: Define the function convertInputToValue, taking input as a string and
returning a float64.
Step 4.1: Parse input to a float64 value, storing the result in result.
Step 4.2: Return result.
Step 5: End

From the algorithmic expression I created the Golang code.

Firstly, the program initializes two variables `value1` and `value2` with the strings “10” and “5.5” respectively, and `operation` with the string “+”. Then, the `calculate` function is called with these variables as arguments. Inside `calculate`, the `convertInputToValue` function is used to convert the string inputs into float64 values. Based on the specified operation, which is determined using a switch statement, one of the helper functions (`addValues`, `subtractValues`, `multiplyValues`, `divideValues`) is called to perform the arithmetic operation on the float64 values. The result is then returned and printed in the `main` function. If there’s an error during the conversion of strings to float64 values, it will be printed, but since error handling is not implemented, the program will continue executing.

Run the code main.go

package main

import (
"fmt"
"strconv"
)

func main() {
value1 := "10"
value2 := "5.5"
operation := "+"
result := calculate(value1, value2, operation)
fmt.Println("Result:", result)
}

// calculate performs the specified operation on the given inputs and returns the result.
func calculate(input1 string, input2 string, operation string) float64 {
floatInput1 := convertInputToValue(input1)
floatInput2 := convertInputToValue(input2)
var result float64
switch operation {
case "+":
result = addValues(floatInput1, floatInput2)
case "-":
result = subtractValues(floatInput1, floatInput2)
case "*":
result = multiplyValues(floatInput1, floatInput2)
case "/":
result = divideValues(floatInput1, floatInput2)
}
return result
}

// convertInputToValue converts the given input string to a float64 value.
func convertInputToValue(input string) float64 {
result, err := strconv.ParseFloat(input, 64)
fmt.Println(err)
return result
}

// addValues adds two float64 values and returns the result.
func addValues(value1, value2 float64) float64 {
return value1 + value2
}

// subtractValues subtracts the second float64 value from the first and returns the result.
func subtractValues(value1, value2 float64) float64 {
return value1 - value2
}

// multiplyValues multiplies two float64 values and returns the result.
func multiplyValues(value1, value2 float64) float64 {
return value1 * value2
}

// divideValues divides the first float64 value by the second and returns the result.
func divideValues(value1, value2 float64) float64 {
return value1 / value2
}

Output:

<nil>
<nil>
Result: 15.5

Unit Test the code from main.go by using main_test.go

package main

import (
"testing"
)

func TestCalculate(t *testing.T) {
input1 := "5"
input2 := "3"
operation := "+"
expected := 8.0

result := calculate(input1, input2, operation)

if result != expected {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestCalculate_Addition(t *testing.T) {
input1 := "10"
input2 := "5"
operation := "+"
expected := 15.0

result := calculate(input1, input2, operation)

if result != expected {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestCalculate_Subtraction(t *testing.T) {
input1 := "10"
input2 := "5"
operation := "-"
expected := 5.0

result := calculate(input1, input2, operation)

if result != expected {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestCalculate_Multiplication(t *testing.T) {
input1 := "10"
input2 := "5"
operation := "*"
expected := 50.0

result := calculate(input1, input2, operation)

if result != expected {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestCalculate_Division(t *testing.T) {
input1 := "10"
input2 := "5"
operation := "/"
expected := 2.0

result := calculate(input1, input2, operation)

if result != expected {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

Test output

After running the test you should see the following output which indicates the test is passed.

Running tool: /opt/homebrew/bin/go test -timeout 30s -run ^TestCalculate$

=== RUN TestCalculate
<nil>
<nil>
--- PASS: TestCalculate (0.00s)
PASS
ok _/Users/akarshseggemu/Development/Go/ 0.177s

The test suite includes multiple unit test functions such as `TestCalculate`, `TestCalculate_Addition`, `TestCalculate_Subtraction`, `TestCalculate_Multiplication`, and `TestCalculate_Division`.

Each function tests different aspects of the `calculate` function, focusing on various arithmetic operations. Within each test function, the `calculate` function is invoked with predefined input values for operands (`input1` and `input2`) and an operation (`operation`). The expected result is also predefined. Subsequently, an if statement compares the actual result returned by `calculate` with the expected result. If there is a mismatch, an error message is logged using `t.Errorf()`, indicating the failure of the test.

These unit tests ensure the correctness of the `calculate` function across different arithmetic operations and input scenarios.

I hope you enjoyed my article on creating a calculator program.

If you like my articles, please follow me on Medium, you can also watch my videos on YouTube and you can also support me by buying me a coffee.

--

--

Akarsh Seggemu, M.Sc.

IT Team Lead | Post graduate Computer Science from TU Berlin | Telugu writings are found in this account https://medium.com/@akarshseggemu_telugu