File tree 1 file changed +54
-0
lines changed
1 file changed +54
-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
+ // shape interface
9
+ type shape interface {
10
+ area () float64
11
+ circumf () float64
12
+ }
13
+
14
+ type square struct {
15
+ length float64
16
+ }
17
+ type circle struct {
18
+ radius float64
19
+ }
20
+
21
+ // square methods
22
+ func (s square ) area () float64 {
23
+ return s .length * s .length
24
+ }
25
+ func (s square ) circumf () float64 { // same as perimeter
26
+ return s .length * 4
27
+ }
28
+
29
+ // circle methods
30
+ func (c circle ) area () float64 {
31
+ return math .Pi * c .radius * c .radius
32
+ }
33
+ func (c circle ) circumf () float64 { // same as perimeter
34
+ return 2 * math .Pi * c .radius
35
+ }
36
+
37
+ func printShapeInfo (s shape ) {
38
+ fmt .Printf ("area of %T is: %0.2f \n " , s , s .area ())
39
+ fmt .Printf ("circumference of %T is: %0.2f \n " , s , s .circumf ())
40
+ }
41
+
42
+ func main () {
43
+ shapes := []shape {
44
+ square {length : 15.2 },
45
+ circle {radius : 7.5 },
46
+ circle {radius : 12.3 },
47
+ square {length : 4.9 },
48
+ }
49
+
50
+ for _ , v := range shapes {
51
+ printShapeInfo (v )
52
+ fmt .Println ("-----" )
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments