Basic calculator in GO language

Hi I am new into the GO programming language.

I was struckeling with the following task;

I was creating a calculator that must be able to perform addition, subtraction, multiplication and division of two given integers.

  •   At the start of the program  it should  ask the user to select a operation like:
      Select operation
     1) Add
     2) Subtract
     3) Multiply
     4) Divide
  • The user should enter there choice.       > ( Enter choice of operation (1/2/3/4):
  • Ask user to enter the first interger                   > (enter first integer)
  • Ask user to enter the second interger             > (enter second integer)

Based on the operation selection of the user it should calculate the value of the two given integers.

Thanks in advance.


Hello !

You can create a function for every operation. Afterwards, in main() read the user input and call the appropriate function.

User input can consist of selection of operation and the number which operation will be applied.

Good luck

func add(int number1, int number2) int{
   return number1 + number2;

func multiply(int number1, int number2) int{
   return number1 * number2;

func divide(int number1, int number2) float{
    if number2 ==  0 {
       panic("you can not divide to 0") //or replace panic with fmt.Println();

   return number1 / number2;

func substract(int number1, int number2) int{
   return number1 - number2;

This are the functions for the calculator. Hope it will help you.

Note that I am still learning Go :wink:
If you spot a mistake, please let me know

Have a nice day :slight_smile:


Thanks for outlining the task.

What exactly have you done so far and what specific coding problem do you have?

It will make it easier for people to help you if you provide examples of the code you have already written and explain what you expect it to do.

Here some coding for the Basic calculator in Go language.
(It,s not perfect !!! could be better…).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.