funcmain() { name := MyString("Sam Anderson") var v VowelsFinder v = name // possible since MyString implements VowelsFinder fmt.Printf("Vowels are %c", v.FindVowels())
type SalaryCalculator interface { CalculateSalary() int }
type Permanent struct { empId int basicpay int pf int }
type Contract struct { empId int basicpay int }
//salary of permanent employee is the sum of basic pay and pf func(p Permanent) CalculateSalary() int { return p.basicpay + p.pf }
//salary of contract employee is the basic pay alone func(c Contract) CalculateSalary() int { return c.basicpay }
/* total expense is calculated by iterating through the SalaryCalculator slice and summing the salaries of the individual employees */ functotalExpense(s []SalaryCalculator) { expense := 0 for _, v := range s { expense = expense + v.CalculateSalary() } fmt.Printf("Total Expense Per Month $%d", expense) }
type SalaryCalculator interface { CalculateSalary() int }
type Permanent struct { empId int basicpay int pf int }
type Contract struct { empId int basicpay int }
type Freelancer struct { empId int ratePerHour int totalHours int }
//salary of permanent employee is sum of basic pay and pf func(p Permanent) CalculateSalary() int { return p.basicpay + p.pf }
//salary of contract employee is the basic pay alone func(c Contract) CalculateSalary() int { return c.basicpay }
//salary of freelancer func(f Freelancer) CalculateSalary() int { return f.ratePerHour * f.totalHours }
/* total expense is calculated by iterating through the SalaryCalculator slice and summing the salaries of the individual employees */ functotalExpense(s []SalaryCalculator) { expense := 0 for _, v := range s { expense = expense + v.CalculateSalary() } fmt.Printf("Total Expense Per Month $%d", expense) }
funcfindType(i interface{}) { switch i.(type) { casestring: fmt.Printf("I am a string and my value is %s\n", i.(string)) caseint: fmt.Printf("I am an int and my value is %d\n", i.(int)) default: fmt.Printf("Unknown type\n") } } funcmain() { findType("Naveen") findType(77) findType(89.98) }