GO | Simple Calculator Project
So, i came to know about this new programming language called Golang, shorten as Go. It was designed at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson, and publicly announced in November of 2009. It is known for the simplicity of its syntax and the efficiency of development that it enables by the inclusion of a large standard library supplying many needs for common projects.
It dosent have the same number of packages as python. But it does shows the its potential to reach above Python or any other programming languages. One thing i noticed when i was working with it, is that it does shows errors or logic errors (sometimes) at the time of saving the file. Cause Go is a complied language. So before running the program, 99% of errors will be solved.
What about the structure? What’s the code organization?
Code organization¶ (From Go.dev)
Go programs are organized into packages. A package is a collection of source files in the same directory that are compiled together. Functions, types, variables, and constants defined in one source file are visible to all other source files within the same package.
A repository contains one or more modules. A module is a collection of related Go packages that are released together. A Go repository typically contains only one module, located at the root of the repository. A file named go.mod there declares the module path: the import path prefix for all packages within the module. The module contains the packages in the directory containing its go.mod file as well as subdirectories of that directory, up to the next subdirectory containing another go.mod file (if any).
Note that you don't need to publish your code to a remote repository before you can build it. A module can be defined locally without belonging to a repository. However, it's a good habit to organize your code as if you will publish it someday.
Each module's path not only serves as an import path prefix for its packages, but also indicates where the go command should look to download it. For example, in order to download the module golang.org/x/tools, the go command would consult the repository indicated by https://golang.org/x/tools (described more here).
An import path is a string used to import a package. A package's import path is its module path joined with its subdirectory within the module. For example, the module github.com/google/go-cmp contains a package in the directory cmp/. That package's import path is github.com/google/go-cmp/cmp. Packages in the standard library do not have a module path prefix.
To simplify;
🧱 Packages
A package is a folder with
.gofiles.Files in the same package can use each other's functions.
Example:
mathutils/add.goandmathutils/subtract.goare in the same package.
📦 Modules
A module is your whole Go project.
It can contain many packages (folders).
You create a module with:
go mod init [module-name]This creates a
go.modfile at the root.
🔗 Import Paths
To use a package, you import it with a path.
If your module is called
github.com/you/myapp, and you have a foldermathutils, the import looks like:goCopy codeimport "github.com/you/myapp/mathutils"For built-in packages like
fmt, just write:goCopy codeimport "fmt"
💡 Key Points
Packages = folders of code.
Module = your project with
go.mod.You can build and run locally — no need for GitHub.
Import paths tell Go where to find code
Thanks to ChatGPT for the simplification.
What makes GO different?
1. Simplicity and Minimalism
Go has a small, clean standard library and very few keywords.
There's one way to do most things — a stark contrast to languages like Python or Ruby.
Avoids complex features like inheritance, generics (until recently), or macros.
2. Built-in Concurrency
Go was designed for modern multicore systems.
It has goroutines (lightweight threads) and channels for safe, easy-to-use concurrency.
Its concurrency model is based on CSP (Communicating Sequential Processes), which is elegant and scalable.
3. Compiled, Fast, and Efficient
Go is statically typed and compiled to native machine code.
Fast compilation times compared to C++ or Java.
Produces a single binary with no external dependencies by default.
4. Garbage Collected
Unlike C or C++, Go has automatic memory management.
Offers a good balance between control and safety.
So i learned some basics of go and done small simple calculator project. I felt easy and more defined way to code. Maybe i will learn more.
Simple calculator Project
package main
import (
"fmt"
)
func Addition(num1 int,num2 int) int {
var sum int
sum = num1 + num2
return sum
}
func Subtration(num1 int,num2 int) int {
var sub int
sub = num1 - num2
return sub
}
func main() {
fmt.Println("Simple maths calculator")
fmt.Println("1. Addition\n2. Subtration\n3. Multiplication\n4. Division")
for{
print("Choice? : ")
var ch int
fmt.Scan(&ch)
if ch==1{
var num1, num2 int
fmt.Println("Enter 1st number:")
fmt.Scan(&num1)
fmt.Println("Enter 2nd number:")
fmt.Scan(&num2)
sum := Addition(num1, num2)
fmt.Println("Sum is: ", sum)
}else if ch == 2{
var num1, num2 int
fmt.Println("Enter 1st number:")
fmt.Scan(&num1)
fmt.Println("Enter 2nd number:")
fmt.Scan(&num2)
sub := Subtration(num1, num2)
fmt.Println("Subtration is: ", sub)
}else if ch == 3{
var num1, num2 int
fmt.Println("Enter 1st number:")
fmt.Scan(&num1)
fmt.Println("Enter 2nd number:")
fmt.Scan(&num2)
mul := num1 * num2
fmt.Println("Multiplication is: ", mul)
}else if ch == 4{
var num1, num2 int
fmt.Println("Enter 1st number:")
fmt.Scan(&num1)
fmt.Println("Enter 2nd number:")
fmt.Scan(&num2)
div := num1 / num2
fmt.Println("Division is: ", div)
}else{
print("Invalid choice")
break
}
}
}
Thanks! Go coding!