Lab 1: Program to display a sample text
Aim:
To find a program to display a sample text in GO.
Procedure:
1. Save the file as [Link].
2. Open a terminal or command prompt.
3. Navigate to the directory containing the file.
4. Run the program using the command:
Program:
package main
import "fmt"
func main() {
[Link]("Hello!")
}
Output:
Hello!
Result:
The program has successfully compiled and verified.
Lab 2: Write a sample program in GO
Aim:
To find a program of displaying a sample program in GO.
Procedure:
1. Save the file as [Link].
2. Open a terminal or command prompt.
3. Navigate to the directory where [Link] is saved
Program:
package main
import "fmt"
func main() {
[Link]("Welcome to GO Programming!")
Output:
Welcome to GO Programming!
Result:
Thus the program has successfully compiled and verified.
Lab 3: Write a program to find the biggest of three numbers
Aim:
Demonstrate a program of finding the biggest among three numbers.
Procedure:
1. Declares 3 hardcoded numbers.
2. Uses simple if-else logic to compare them.
3. Stores the largest number in a variable.
4. Prints out the largest number.
Program:
package main
import "fmt"
func main() {
a := 10
b := 25
c := 15
var biggest int
if (a > b && a > c){
[Link](“The biggest number is:”,a)
else if (b > c){
[Link](“The biggest number is:”,b)
else {
[Link]("The biggest number is:”,c)
Output:
The biggest number is: 25
Result:
Thus the above program has successfully compiled and verified.
Lab 4: (i) Program to display all Prime Numbers between 1 to 100
Aim:
To find a program to display all prime numbers between 1 to 100 using
GO.
Procedure:
1. The program defines a helper function isPrime(num int):
Returns false for numbers less than or equal to 1.
Checks for divisibility from 2 to √num.
If any divisor is found, the number is not prime.
2. In the main() function:
A loop runs from 1 to 100.
Each number is passed to the isPrime() function.
If the number is prime, it is printed.
Program:
package main
import "fmt"
func isPrime(num int) bool {
if num <= 1 {
return false
for i := 2; i*i <= num; i++ {
if num%i == 0 {
return false
return true
}
func main() {
[Link]("Prime numbers between 1 and 100 are:")
for i := 1; i <= 100; i++ {
if isPrime(i) {
[Link]("%d ", i)
[Link]()
Output:
Prime numbers between 1 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Result:
Thus the above program has successfully compiled and verified.
Lab 4: (ii) Program to display a Pattern
Aim:
To demonstrate a program to display a Pattern using GO.
Procedure:
1. Outer loop (i) runs from 1 to rows (number of lines).
2. Inner loop (j) prints * as many times as the current row number.
3. After each row, a newline is printed.
4. Looping (nested for loops)
5. Basic output with [Link]() and [Link]()
6. Pattern logic (increasing stars per row)
Program:
package main
import "fmt"
func main() {
rows := 5
[Link]("Right-angled Triangle Pattern:")
for i := 1; i <= rows; i++ {
for j := 1; j <= i; j++ {
[Link]("* ")
[Link]()
Output:
Right-angled Triangle Pattern:
**
***
****
*****
Result:
Thus the above program has successfully compiled and verified.
Lab 5: (i) Program for Two-Dimensional array matrix multiplication
Aim:
To display a two-dimensional array matrix multiplication using GO.
Procedure:
1. Define two 2D arrays (A, B).
2. Use 3 nested loops:
3. Outer two loops for rows and columns of the result.
4. Innermost loop for the dot product of row of A and column of B.
5. Store the result in a third matrix.
6. Print all matrices using a helper function
Program:
package main
import "fmt"
func main() {
A := [2][2]int{{1, 2}, {3, 4}}
B := [2][2]int{{5, 6}, {7, 8}}
var result [2][2]int
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
result[i][j] = 0
for k := 0; k < 2; k++ {
result[i][j] += A[i][k] * B[k][j]
}
[Link]("Matrix A:")
printMatrix(A)
[Link]("Matrix B:")
printMatrix(B)
[Link]("Result of A * B:")
printMatrix(result)
func printMatrix(matrix [2][2]int) {
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
[Link]("%d ", matrix[i][j])
[Link]()
Output:
Matrix A:
12
34
Matrix B:
56
78
Result of A * B:
19 22
43 50
Result:
Thus the above program has successfully compiled and verified.
Lab 5: (ii) Write a program to implement jump statement
Aim:
To display a program to implement jump statement using GO.
Procedure:
1. Uses break to exit a loop early.
2. Uses continue to skip certain iterations.
3. Uses goto to jump to a labeled line in the program.
Program:
package main
import "fmt"
func main() {
[Link]("Demonstrating break statement:")
for i := 1; i <= 10; i++ {
if i == 5 {
break // exits the loop when i is 5
}
[Link](i, " ")
}
[Link]("\n\nDemonstrating continue statement:")
for i := 1; i <= 5; i++ {
if i == 3 {
continue // skips the rest of the loop when i is 3
}
[Link](i, " ")
}
[Link]("\n\nDemonstrating goto statement:")
[Link]("Start")
goto EndLabel // jumps to the EndLabel
[Link]("This line will be skipped") // skipped
[Link]("End")
}
Output:
Demonstrating break statement:
1234
Demonstrating continue statement:
1245
Demonstrating goto statement:
Start
End
Result:
Thus the above program has successfully compiled and verified.
Lab 6: Find Min and Max using Function
Aim:
To find the minimum and maximum of two numbers using a function in Go.
Procedure:
1. Define a function that takes two integers.
2. Use an if condition to compare.
3. Return the smaller and larger number.
4. Print the result in main().
Program:
package main
import "fmt"
func findMinMax(a, b int) (int, int) {
if a < b {
return a, b
}
return b, a
}
func main() {
min, max := findMinMax(10, 25)
[Link]("Min:", min)
[Link]("Max:", max)
}
Output:
Min: 10
Max: 25
Result:
The program successfully found the minimum and maximum values using a function.
Lab 7: Find NCR using Recursion Function
Aim:
To calculate NCR (n Choose r) using recursive function in Go.
Procedure:
1. Write a recursive factorial function.
2. Use the formula: NCR = n! / (r! * (n - r)!)
3. Call and print the result in main().
Program:
package main
import "fmt"
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}
func ncr(n, r int) int {
return factorial(n) / (factorial(r) * factorial(n-r))
}
func main() {
var n, r int = 5, 2
[Link]("NCR is:", ncr(n, r))
}
Output:
NCR is: 10
Result:
The program successfully calculated the value of NCR using recursion.
Lab 8: Swap Two Numbers using Function Returning Two Values
Aim:
To swap two numbers using a function that returns two values.
Procedure:
1. Define a function with two parameters.
2. Return the values in reverse.
3. Swap values using multiple assignment.
Program:
package main
import "fmt"
func swap(a, b int) (int, int) {
return b, a
}
func main() {
x, y := 10, 20
[Link]("Before swap:", x, y)
x, y = swap(x, y)
[Link]("After swap:", x, y)
}
Output:
Before Swap: 10 20
After Swap: 20 10
Result:
The program successfully swapped the two numbers using a function.
Lab 9: Employee Details using Struct
Aim:
To define and display employee details using a struct in Go.
Procedure:
1. Define a struct named Employee.
2. Create an object of that struct.
3. Assign and print its values.
Program:
package main
import "fmt"
type Employee struct {
id int
name string
salary float64
}
func main() {
emp := Employee{101, "Alice", 25000}
[Link]("Employee ID :", [Link])
[Link]("Employee Name :", [Link])
[Link]("Salary :", [Link])
}
Output:
Employee ID : 101
Employee Name : Alice
Salary : 25000
Result:
The program successfully displayed employee details using struct.
Lab 10: Use of Pointer in Function
Aim:
To demonstrate the use of pointers in a function in Go.
Procedure:
1. Declare a variable and a pointer to it.
2. Pass the address to a function.
3. Modify the value using the pointer.
Program:
package main
import "fmt"
func changeValue(x *int) {
*x = 50
}
func main() {
a := 10
[Link]("Before:", a)
changeValue(&a)
[Link]("After:", a)
}
Output:
Before: 10
After: 50
Result:
The program successfully demonstrated pointer usage by modifying a variable from another
function.
Lab 11: Program to demonstrate writing data into a file and reading
data from a file.
Aim:
To find a program to demonstrate writing data into a file and reading data from a file using
GO.
Procedure:
1. Use os Create() to create or open a file named [Link].
2. Write a string to the file using WriteString().
3. Read the file content using [Link]().
4. Display the content on the console with [Link]().
Program:
package main
import (
"fmt"
"os"
)
func main() {
file, err := [Link]("[Link]")
if err != nil {
[Link]("Error:", err)
return
}
defer [Link]()
[Link]("Hello from Go!")
data, err := [Link]("[Link]")
if err != nil {
[Link]("Error:", err)
return
}
[Link]("File content:", string(data))
}
Output:
File content: Hello from Go!
Result:
The program has successfully writing data into a file and reading data from a file.
Lab 12: Program to demonstrate interface.
Aim: To illustrate how interfaces work in GO by defining a Speaker interface and
implementing it with a Dog type.
Procedure
1. Define the Speaker interface with a method Speak().
2. Create a Dog struct and implement the Speak() method.
3. Assign a Dog instance to a variable of type Speaker.
4. Call the Speak() method using the interface reference.
Program:
package main
import "fmt"
type Speaker interface {
Speak()
}
type Dog struct{}
func (d Dog) Speak() {
[Link]("Woof!")
}
func main() {
var s Speaker = Dog{}
[Link] ()
}
Output:
Woof!
Result:
The program has successfully demonstrated interface.
Lab 13: Program to demonstrate classes.
Aim:
To simulate class behaviour in Go using struct and methods.
Procedure:
1. Create a struct Car with fields: Brand, Model, and Year.
2. Define a method DisplayInfo() for the Car struct.
3. Instantiate a Car object with sample data.
4. Call the method to print the car's details.
Program:
package main
import "fmt"
type Car struct {
Brand string
Model string
Year int
}
func (c Car) DisplayInfo() {
[Link]("Car: %s %s (%d)\n", [Link], [Link], [Link])
}
func main() {
myCar := Car{Brand: "Toyota", Model: "Camry", Year: 2022}
[Link]()
}
Output:
Car: Toyota Camry (2022)
Result:
The program has successfully demonstrated classes.
Lab 14: Program to calculate the area of a rectangle
Aim:
To write a Go program that calculates and displays the area of a rectangle based on
predefined length and width.
Procedure:
1. Declare variables length and width with float values.
2. Calculate the area using the formula length * width.
3. Use fmt. Printf to print the dimensions and the result.
4. Format the output to show two decimal places.
Program:
package main
import "fmt"
func main() {
length := 10.0
width := 5.0
area := length * width
[Link]("Length: %.2f\n", length)
[Link]("Width: %.2f\n", width)
[Link]("Area of the rectangle: %.2f\n", area)
}
Output:
Length: 10.00
Width: 5.00
Area of the rectangle: 50.00
Result:
The program has successfully calculated and displays the area of a rectangle based on
predefined length and width.
Lab 15: Program to calculate simple and compound interest
Aim:
To calculate and display simple and compound interest using principal, rate, and time values
in Go.
Procedure:
1. Define the principal amount, interest rate, and period as float variables.
2. Use the formula (P * R * T) / 100 to compute simple interest.
3. Use [Link] to calculate compound interest: P * [(1 + R/100)^T – 1].
4. Print both interest values formatted to two decimal places.
Program:
package main
import (
"fmt"
"math"
)
func main() {
principal := 2000.0
rate := 5.0
time := 2.0
simpleInterest := (principal * rate * time) / 100
compoundInterest := principal * ([Link](1+(rate/100), time) - 1)
[Link]("Simple Interest: ₹%.2f\n", simpleInterest)
[Link]("Compound Interest: ₹%.2f\n", compoundInterest)
}
Output:
Simple Interest: ₹200.00
Compound Interest: ₹205.00
Result:
The program has successfully calculated simple and compound interest.