Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b7d9bf

Browse files
author
mostafa
committed
interface
1 parent 43e6523 commit 1b7d9bf

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

‎advanced/interface/basic.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
11
package main
22

3-
import (
4-
"fmt"
5-
)
3+
import "fmt"
64

7-
//interface definition
8-
type VowelsFinder interface {
9-
FindVowels() []rune
5+
type SalaryCalculator interface {
6+
CalculateSalary() int
107
}
118

12-
type MyString string
9+
type Permanent struct {
10+
JobType string
11+
Salary int
12+
}
1313

14-
//MyString implements VowelsFinder
15-
func (ms MyString) FindVowels() []rune {
16-
var vowels []rune
17-
for _, rune := range ms {
18-
if rune == 'a' || rune == 'e' || rune == 'i' || rune == 'o' || rune == 'u' {
19-
vowels = append(vowels, rune)
20-
}
21-
}
22-
return vowels
14+
type Contractual struct {
15+
JobType string
16+
Salary int
17+
}
18+
19+
func (p Permanent) CalculateSalary() int {
20+
return p.Salary
21+
}
22+
23+
func (c Contractual) CalculateSalary() int {
24+
return c.Salary + 1000
25+
}
26+
27+
func TotalExpense(s SalaryCalculator) {
28+
fmt.Println(s.CalculateSalary())
2329
}
2430

2531
func main() {
26-
name := MyString("Sam Anderson")
27-
var v VowelsFinder
28-
v = name // possible since MyString implements VowelsFinder
29-
fmt.Printf("Vowels are %c", v.FindVowels())
32+
fmt.Println("welcome")
33+
34+
empPermanent := Permanent{
35+
JobType: "permanent",
36+
Salary: 2000,
37+
}
38+
39+
TotalExpense(empPermanent)
40+
41+
empContarctual := Contractual{
42+
JobType: "contract",
43+
Salary: 5000,
44+
}
3045

46+
TotalExpense(empContarctual)
3147
}

‎hello.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ import "fmt"
55
func main() {
66
var name string = "Golang" + " " + "Lovers"
77
fmt.Println("Hello, ", name)
8-
98
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /