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 4ce9f18

Browse files
Interfaces
1 parent c42d566 commit 4ce9f18

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

‎04-constants/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ func main() {
1111
const country, code = "India", 91
1212

1313
const (
14-
employeeId string = "E101"
15-
salary float64 = 50000.0
14+
employeeId string = "E101"
15+
salary float64 = 50000.0
1616
)
1717

1818
// Typed Constant
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
// Go Interface - `Shape`
9+
type Shape interface {
10+
Area() float64
11+
Perimeter() float64
12+
}
13+
14+
// ==================================================================
15+
// Struct type `Circle` - implements the `Shape` interface implicitly
16+
type Circle struct {
17+
Radius float64
18+
}
19+
20+
func (c Circle) Area() float64 {
21+
return math.Pi * c.Radius * c.Radius
22+
}
23+
24+
func (c Circle) Perimeter() float64 {
25+
return 2 * math.Pi * c.Radius
26+
}
27+
28+
func (c Circle) Diameter() float64 {
29+
return 2 * c.Radius
30+
}
31+
32+
// ====================================================================
33+
// Struct type `Rectangle` - implements the `Shape` interface implicitly
34+
type Rectangle struct {
35+
Length, Width float64
36+
}
37+
38+
func (r Rectangle) Area() float64 {
39+
return r.Length * r.Width
40+
}
41+
42+
func (r Rectangle) Perimeter() float64 {
43+
return 2 * (r.Length + r.Width)
44+
}
45+
46+
// ==================================================================================
47+
// Generic function to calculate the total area of multiple shapes of different types
48+
func CalculateTotalArea(shapes ...Shape) float64 {
49+
totalArea := 0.0
50+
for _, s := range shapes {
51+
totalArea += s.Area()
52+
}
53+
return totalArea
54+
}
55+
56+
// ====================================================================
57+
// Interfaces can also be used as fields
58+
type MyDrawing struct {
59+
shapes []Shape
60+
bgColor string
61+
fgColor string
62+
}
63+
64+
func (drawing MyDrawing) Area() float64 {
65+
totalArea := 0.0
66+
for _, s := range drawing.shapes {
67+
totalArea += s.Area()
68+
}
69+
return totalArea
70+
}
71+
72+
// ==================================================================
73+
func main() {
74+
var s Shape = Circle{5.0}
75+
fmt.Printf("Shape Type = %T, Shape Value = %v\n", s, s)
76+
fmt.Printf("Area = %f, Perimeter = %f\n\n", s.Area(), s.Perimeter())
77+
78+
var s1 Shape = Rectangle{4.0, 6.0}
79+
fmt.Printf("Shape Type = %T, Shape Value = %v\n", s1, s1)
80+
fmt.Printf("Area = %f, Perimeter = %f\n", s1.Area(), s1.Perimeter())
81+
82+
totalArea := CalculateTotalArea(Circle{2}, Rectangle{4, 5}, Circle{10})
83+
fmt.Println("Total area = ", totalArea)
84+
85+
drawing := MyDrawing{
86+
shapes: []Shape{
87+
Circle{2},
88+
Rectangle{3, 5},
89+
Rectangle{4, 7},
90+
},
91+
bgColor: "red",
92+
fgColor: "white",
93+
}
94+
95+
fmt.Println("Drawing", drawing)
96+
fmt.Println("Drawing Area = ", drawing.Area())
97+
98+
s = Circle{5}
99+
fmt.Printf("(%v, %T)\n", s, s)
100+
fmt.Printf("Shape area = %v\n", s.Area())
101+
102+
s = Rectangle{4, 7}
103+
fmt.Printf("(%v, %T)\n", s, s)
104+
fmt.Printf("Shape area = %v\n", s.Area())
105+
106+
}

‎Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@
4646

4747
### Methods and Interfaces
4848

49-
+ [Golang Methods Tutorial with Examples](https://www.callicoder.com/golang-methods-tutorial/)
49+
+ [Golang Methods Tutorial with Examples](https://www.callicoder.com/golang-methods-tutorial/)
50+
51+
+ [Golang Interfaces Tutorial with Examples](https://www.callicoder.com/golang-interfaces/)

0 commit comments

Comments
(0)

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