Skip to content

Commit adcf760

Browse files
committed
add interfaces folder
0 parents  commit adcf760

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

interfaces/shape/main.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type triangle struct {
6+
height float64
7+
base float64
8+
}
9+
10+
type square struct {
11+
sideLength float64
12+
}
13+
14+
type shape interface {
15+
getArea() float64
16+
}
17+
18+
func main() {
19+
tr := triangle{
20+
height: 23,
21+
base: 46,
22+
}
23+
sq := square{
24+
sideLength: 44,
25+
}
26+
27+
printArea(tr)
28+
printArea(sq)
29+
}
30+
31+
func (s square) getArea() float64 {
32+
return s.sideLength * s.sideLength
33+
}
34+
35+
func (t triangle) getArea() float64 {
36+
return 0.5 * t.base * t.height
37+
}
38+
39+
func printArea(s shape) {
40+
fmt.Println(s.getArea())
41+
}

0 commit comments

Comments
 (0)