Program to demonstrate string functions
package main
import (
"fmt"
"strings"
)
// CompareStrings compares two strings and prints the comparison result.
func CompareStrings(s1, s2 string) {
[Link]([Link](s1, s2))
}
// ContainsString checks if a substring is present in a given string.
func ContainsString(text, substring string) {
[Link]([Link](text, substring))
}
// ReplaceString replaces the first occurrence of oldChar with newChar in a
string.
func ReplaceString(text, oldChar, newChar string) {
[Link]("Old String:", text)
replacedText := [Link](text, oldChar, newChar, 1)
[Link]("New String:", replacedText)
}
// JoinStrings joins a slice of words into a single string with spaces.
func JoinStrings(words []string) {
message := [Link](words, " ")
[Link](message)
}
// SplitString splits a string by spaces and prints the resulting slice.
func SplitString(text string) {
splittedString := [Link](text, " ")
[Link](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 := [Link] + [Link] + [Link]
return [Link] + [Link] - 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 := [Link]()
[Link]("Employee: %s (ID: %d)\n", [Link], [Link])
[Link](" Basic Salary: %.2f\n", [Link])
[Link](" HRA: %.2f\n", [Link])
[Link](" PF: %.2f\n", [Link])
[Link](" ESI: %.2f\n", [Link])
[Link](" Other Deductions: %.2f\n", [Link])
[Link](" Net Salary: %.2f\n", netSalary)
[Link]("-------------------")
}
}
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
[Link]("Addre0073s of num:",&num)
[Link]("Value of num:",num)
ptr = &num
[Link]("\nAddress of pointer ptr:",ptr)
[Link]("Content of pointer ptr:",*ptr)
num1 = 11
[Link]("\nAddress of pointer ptr:",ptr)
[Link]("Content of pointer ptr:",*ptr)
*ptr = 2
[Link]("\nAddress of num:",&num)
[Link]("Value of num:",num)
res=num+num1
[Link]("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