File tree 1 file changed +68
-0
lines changed
1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "math"
6
+ )
7
+
8
+ type rectangle struct {
9
+ a , b float64
10
+ }
11
+
12
+ func (r rectangle ) area () float64 {
13
+ return r .a * r .b
14
+ }
15
+
16
+ func (r rectangle ) circumference () float64 {
17
+ return 2 * (r .a + r .b )
18
+ }
19
+
20
+ type circle struct {
21
+ r float64
22
+ }
23
+
24
+ func (c circle ) area () float64 {
25
+ return math .Pi * c .r * c .r
26
+ }
27
+
28
+ func (c circle ) circumference () float64 {
29
+ return 2 * math .Pi * c .r
30
+ }
31
+
32
+ func (c circle ) diameter () float64 {
33
+ return 2 * c .r
34
+ }
35
+
36
+ type shape interface {
37
+ area () float64
38
+ circumference () float64
39
+ }
40
+
41
+ func interfaceFunc (i shape ) {
42
+ fmt .Println (i )
43
+ fmt .Println (i .area ())
44
+ fmt .Println (i .circumference ())
45
+ fmt .Printf ("%T" , i )
46
+ fmt .Println ()
47
+ }
48
+
49
+ func main () {
50
+
51
+ /* r1 := rectangle{3, 8}
52
+ fmt.Println("Area: ", r1.area())
53
+ fmt.Println("Circumference: ", r1.circumference())
54
+
55
+ fmt.Println()
56
+ interfaceFunc(r1) */
57
+
58
+ r1 := rectangle {3 , 8 }
59
+ interfaceFunc (r1 )
60
+
61
+ fmt .Println ()
62
+
63
+ c1 := circle {5 }
64
+ interfaceFunc (c1 )
65
+
66
+ }
67
+
68
+ // concrete data type
You can’t perform that action at this time.
0 commit comments