# Golang program to demonstrate a simple module

In this Go language article, we will write programs to demonstrate a simple module. A module in Go is called as the collection of packages namely we can call it packages and sub-packages where the root file will be called as go.mod. In this program we will demonstrate a module using three different examples.

## Syntax

func make ([] type, size, capacity)


The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

## Example1: Golang program to demonstrate a simple module using basic printing

In this example, we will write a Golang program to demonstrate a simple module by printing a simple statement ("Hello, alexaā€¯).

package main

import (
"fmt"
)

func main() {
fmt.Println("Hello, alexa!")
}


## Output

Hello, alexa!


## Example 2

In this example, we will calculate factorial of a number to demonstrate a simple module. We will calculate the factorial using recursion and the Output will be the factorial value returned to the function

package main

import (
"fmt"
)

func factorial_number(value int) int {
if value == 0 {
return 1
}
return value * factorial_number(value-1)
}

func main() {
number := 4
Output := factorial_number(number)
fmt.Printf("The factorial of %d is %d\n", number, Output)
}


## Output

The factorial of 4 is 24


## Example 3

In this method we will print fibonacci numbers to demonstrate simple module. We will print fibonacci numbers using iterative method and store them in an array which will be returned to the function. The Output will be an array of fibonacci numbers printed on the console.

package main

import (
"fmt"
)

func fibonacci_number(n int) []int {
fibo := make([]int, n)
fibo[0], fibo[1] = 0, 1
for i := 2; i < n; i++ {
fibo[i] = fibo[i-1] + fibo[i-2]
}
return fibo
}

func main() {
length := 6
Output := fibonacci_number(length)
fmt.Printf("The first %d numbers in the Fibonacci sequence are %v\n", length, Output)
}


## Output

The first 6 numbers in the Fibonacci sequence are [0 1 1 2 3 5]


## Conclusion

We executed the program of demonstrating a simple module using three examples. In the first example we printed a simple statement using Println function, in the second example we printed factorial of a number and in the third example we printed fibonacci numbers.

Updated on: 03-Apr-2023

53 Views