[Go] Variable, function and package

Go has some similarity with python and a lot are different, fortunately during my university I was forced to take up foundation of programming which I have learned C.

So below are the codes which I have put in comments to explain the statements.
simplemath package

package simplemath // my custom 2 integers math package.

// Add(), the A is capital hence can be "exported" to other package out of simplemath package.
// (a, b int), this says variable a and b are int type. then int after the paranthesis is to
// tell Go that the return value is an int.
// since i did not specify the return variable name, i need to return the result a + b
func Add(a, b int) int {
	return a + b

func Subtract(a, b int) int {
	return a - b

// This function has declared the return variable name - product which is type int.
// hence my multiplication has to be stored in the product variable
// but my return statement can just be return itself.
func Multiply(a, b int) (product int) {
	product = a * b

func Divide(a, b int) (division int) {
	division = a / b

Main package that imports simplemath and runs it

package main

import (
	"fmt" // bulit-in formatting package
	"lab1/simplemath" // custom math package

func main() {
	/* assignment of values to variables can be done in several ways

	// Declare a variable addition and explicitly give int as the data type.
	// If int type is not specified then Go will assume int64 if on 64-bit system
	// and int32 if on 32-bit system
	var addition int
	// assign the result from the package function into the variable.
	// = is used for assignment only after the variable addition is declared.
	addition = simplemath.Add(5, 10)

	// Else you should assign value in this way.
	// This is a shorthand for var multiplication = simplemath.Multiply(2, 8)
	// When := is used, it declares the variable and do the assignment at the same time.
	multiplication := simplemath.Multiply(2, 8)

	// This is to declare a variable and explicitly declare the data type as int.
	// the shorthand for this is division := simplemath.Divide(10, 2)
	var division int = simplemath.Divide(10, 2)

	// This is another way to declare and assign values to variable without declaring data type.
	// Base on the return value the data type is figured by Go.
	var subtraction = simplemath.Subtract(20, 5)

	fmt.Printf is used to format the string.
	%d is decimal, base 10, numbers that are divisible by 10, if you see 0 at the trailing number it is sure a decimal.
	each number is between 0 and 9.
	Example 70 = 7x 10^1 + 0 x 10^0
	Another Example 810 = 8 x 10^2 + 1 x 10^1 + 0 x 10^0
	%o this is octal, base 8, each number between 0 and 7,
	example octal of 144 = 1 x 8^2 + 4 x 8^1 + 4 x 8^0 is a decimal of 100.
	%b is base 2, only between 0 and 1. Example 1001 = 1 x 2^3 + 0 x 2^2 + 0 x 2^1 + 1 x 2^0 = decimal 9
	%t is boolean either True or False.
	See https://golang.org/pkg/fmt/
	fmt.Printf("Addition is %d\n", addition)
	fmt.Printf("Multiplication is %d\n", multiplication)
	fmt.Printf("Division is %d\n", division)
	fmt.Printf("Subtraction is %d", subtraction)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s