0% found this document useful (0 votes)
3 views

go program(2)

The document contains three separate Go programs demonstrating string functions, employee salary calculations, and pointer usage. The string functions include comparisons, substring checks, replacements, joining, and splitting strings. The employee salary program calculates net salaries based on various deductions, while the pointer program illustrates memory address manipulation and basic arithmetic operations.

Uploaded by

techxtream03
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

go program(2)

The document contains three separate Go programs demonstrating string functions, employee salary calculations, and pointer usage. The string functions include comparisons, substring checks, replacements, joining, and splitting strings. The employee salary program calculates net salaries based on various deductions, while the pointer program illustrates memory address manipulation and basic arithmetic operations.

Uploaded by

techxtream03
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program to demonstrate string functions

package main

import (
"fmt"
"strings"
)

// CompareStrings compares two strings and prints the comparison result.


func CompareStrings(s1, s2 string) {
fmt.Println(strings.Compare(s1, s2))
}

// ContainsString checks if a substring is present in a given string.


func ContainsString(text, substring string) {
fmt.Println(strings.Contains(text, substring))
}

// ReplaceString replaces the first occurrence of oldChar with newChar in a


string.
func ReplaceString(text, oldChar, newChar string) {
fmt.Println("Old String:", text)
replacedText := strings.Replace(text, oldChar, newChar, 1)
fmt.Println("New String:", replacedText)
}

// JoinStrings joins a slice of words into a single string with spaces.


func JoinStrings(words []string) {
message := strings.Join(words, " ")
fmt.Println(message)
}

// SplitString splits a string by spaces and prints the resulting slice.


func SplitString(text string) {
splittedString := strings.Split(text, " ")
fmt.Println(splittedString)
}

func main() {
// String Comparison
CompareStrings("Hello", "World") // -1
CompareStrings("World", "Hello") // 1 (Fixed this line)
CompareStrings("Hello", "Hello") // 0

// Checking String Presence


ContainsString("Go Programming", "Go") // true
ContainsString("Go Programming", "Golang") // false

// String Replace
ReplaceString("Fall", "a", "e")

// String Join
words := []string{"I", "love", "Golang"}
JoinStrings(words) // "I love Golang"

// String Split
SplitString("I Love Golang") // ["I", "Love", "Golang"]
}

Program to Calculate Employee Salary


package main

import (
"fmt"
)
// Employee struct to store employee information
type Employee struct {
ID int
Name string
Department string
BasicSalary float64
HRA float64
PF float64
ESI float64
OtherDeductions float64
}

// CalculateNetSalary calculates the net salary


func (e *Employee) CalculateNetSalary() float64 {
totalDeductions := e.PF + e.ESI + e.OtherDeductions
return e.BasicSalary + e.HRA - totalDeductions
}

func main() {
// Create some sample employees
employees := []Employee{
{ID: 1, Name: "John Doe", Department: "IT", BasicSalary: 50000, HRA: 20000, PF:
6000, ESI: 1000, OtherDeductions: 0},
{ID: 2, Name: "Jane Smith", Department: "HR", BasicSalary: 60000, HRA: 25000, PF:
7200, ESI: 1200, OtherDeductions: 0},
}

// Iterate through employees and calculate their net salaries


for _, employee := range employees {
netSalary := employee.CalculateNetSalary()
fmt.Printf("Employee: %s (ID: %d)\n", employee.Name, employee.ID)
fmt.Printf(" Basic Salary: %.2f\n", employee.BasicSalary)
fmt.Printf(" HRA: %.2f\n", employee.HRA)
fmt.Printf(" PF: %.2f\n", employee.PF)
fmt.Printf(" ESI: %.2f\n", employee.ESI)
fmt.Printf(" Other Deductions: %.2f\n", employee.OtherDeductions)
fmt.Printf(" Net Salary: %.2f\n", netSalary)
fmt.Println("-------------------")
}
}

Employee: John Doe (ID: 1)


Basic Salary: 50000.00
HRA: 20000.00
PF: 6000.00
ESI: 1000.00
Other Deductions: 0.00
Net Salary: 63000.00
-------------------
Employee: Jane Smith (ID: 2)
Basic Salary: 60000.00
HRA: 25000.00
PF: 7200.00
ESI: 1200.00
Other Deductions: 0.00
Net Salary: 76600.00
-------------------

=== Code Execution Successful ===

Program to demonstrate Pointer

package main
import "fmt"

func main() {
var num int
var num1 int
var res int
var ptr *int

num = 22
fmt.Println("Addre0073s of num:",&num)
fmt.Println("Value of num:",num)

ptr = &num
fmt.Println("\nAddress of pointer ptr:",ptr)
fmt.Println("Content of pointer ptr:",*ptr)

num1 = 11
fmt.Println("\nAddress of pointer ptr:",ptr)
fmt.Println("Content of pointer ptr:",*ptr)

*ptr = 2
fmt.Println("\nAddress of num:",&num)
fmt.Println("Value of num:",num)
res=num+num1
fmt.Println("Addition of Two value",res)

Output
Address of num: 0xc0000120b0
Value of num: 22

Address of pointer ptr: 0xc0000120b0


Content of pointer ptr: 22

Address of pointer ptr: 0xc0000120b0


Content of pointer ptr: 22

Address of num: 0xc0000120b0


Value of num: 2
Addition of Two value 13

You might also like