You can check the complete code for the sample program here

Introduction to Go and Why It's Interesting

Go is an interesting open-source programming language with a medium learning curve. What intrigued me about Go is that Docker and Kubernetes are written in it.

Go is popular for server-side projects and writing tools because its binaries are statically linked.

If you don't have Go installed, you can download it here.

Understanding Go Packages and Modules

Go programs are organized into packages. Packages contain a collection of code files within the same directory. Go releases a collection of packages as a module. A Go repository includes one or more modules.

Let's consider building a simple two-number calculator.
Go init mod example.com/calculator
Go init mod command creates a Go module with go.mod file in the current directory

Create a file named main.go in the directory containing your go.mod file. The program's execution begins in the package main.

package main

import (
    "fmt"
    "log"
    "os"
)

A package is identified by the word that follows the package statement in the code file.

You can group imports into parenthesized, “factored” import statements. I made a mistake of using curly braces and a comma; it did not build 😭

Implementing the getResult Function

func getResult(x float32, op string, y float32) (float32, error){
    switch op {
    case "+":
        return operator.Add(x,y), nil
    case "-":
        return operator.Sub(x,y), nil
    case "/":
        if y == 0{
            return 0, fmt.Errorf("getResult: cannot divide by zero")
        }
        return operator.Div(x, y), nil
    case "*":
        return operator.Mult(x,y), nil
    default:
        return 0, fmt.Errorf("getResult: invalid operation")
    }   
}

Go simplifies public and private methods by exporting only capitalized names. In the example above, the operator methods are called from a custom local module. You would also notice that the method name getResult starts with a lowercase letter, meaning it should be local (package-private).

When passing arguments or creating variables, the name comes before the type. Consider the string variable assignment below:
var name string = "Peter"

The main Function: Program Execution

func main (){
    fmt.Println("A simple two number Calculator")

    x, err := getFloat()
    if err != nil{
        log.Fatal(err)
        os.Exit(1)
    }

    y, err := getFloat()
    if err != nil{
        log.Fatal(err)
        os.Exit(1)
    }

    op, err := getOperation()
    if err != nil{
        log.Fatal(err)
        os.Exit(1)
    }

    result, err := getResult(x, op, y)
    if err != nil{
        log.Fatal(err)
        os.Exit(1)
    }

    fmt.Printf("The result of %v %v %v = %v", x, op, y, result)
}

Error Handling in Go

You have to be careful when writing programs in Go; in the above, you may see a lot of error handling statements. Error handling in Go is handled explicitly and in a type-driven way using the error type. The developer would have to cover possible error cases—for instance, the simple calculator checks user input for a valid signed number type.

Managing Dependencies: Adding External Modules

Adding a module dependency:

The go get command allows us to add an external dependency on a package in the current module's directory.

$ go get golang.org/x/example/hello/reverse

Managing Dependencies: Importing Local Modules

What if we created another module that we want to import into our current module? We could use the go mod edit command to allow Go to navigate to the directory of the module to be imported.

go mod edit -replace example.com/greetings=../greetings

Next, we use the go tidy command in the directory receiving the import, which ensures that the go.mod file matches the source code in the module.

go mod tidy -v

Running Your Go Calculator

To run the code and be able to use the calculator, navigate to the directory of your main package file and use the go run command.

$ go run .
A simple two number Calculator
Enter a decimal number: 1
Enter a decimal number: 2
Enter any sign among the following: '+', '-', '*', '/': +
The result of 1 + 2 = 3

Building an Executable

To create a binary executable, you run either go build . or go install . in the directory with the Go main package.

The go build command compiles the packages and their dependencies, but it doesn't install the results. You would be able to run the executable as shown below.

$ ./main
A simple two number Calculator
Enter a decimal number:

Installing the Executable and Setting Up Environment Variables

The go install command compiles and installs the packages. Running the install command in the directory of the main package will let Go compile and place the executable in the directory specified by the GOBIN environment variable or its default.

Use the command below to set the GOBIN default directory:
On Unix:

go env -w GOBIN=/path/to/your/bin

On Windows

$ go env -w GOBIN=C:\path\to\your\bin

To unset a variable previously set by go env -w, use go env -u.

Next, we add the install directory to our PATH environment variable to make running binaries easier.

On Unix:

$ export PATH=$PATH:/path/to/your/install/directory

On Windows:

$ set PATH=%PATH%;C:\path\to\your\install\directory

Running the Installed Binary

This allows you run the program with just the main package file name as shown below.

$ main
A simple two number Calculator
Enter a decimal number: 1
Enter a decimal number: 2
Enter any sign among the following: '+', '-', '*', '/': x
2025/04/26 05:45:16 getResult: invalid operation

Conclusion

This article provides a basic introduction to creating a simple command-line calculator in Go, covering module setup, program structure, basic arithmetic logic, error handling, dependency management, and building/running the application.