-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2778 from arcAman07/master
Calculating area of shapes using Interfaces in go.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
Program's_Contributed_By_Contributors/Golang Programs/areaFigure.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
type circle struct { | ||
radius float64 | ||
} | ||
type square struct { | ||
side float64 | ||
} | ||
type rectangle struct { | ||
length, width float64 | ||
} | ||
func (c circle) area() float64 { | ||
return math.Pi * c.radius * c.radius | ||
} | ||
func (s square) area() float64 { | ||
return s.side * s.side | ||
} | ||
func (r rectangle) area() float64 { | ||
return r.length * r.width | ||
} | ||
type shape interface { | ||
area() float64 | ||
} | ||
func info(s shape) { | ||
fmt.Println("Area:", s.area()) | ||
} | ||
func main() { | ||
c := circle{radius: 5} | ||
s := square{side: 5} | ||
r := rectangle{length: 5, width: 10} | ||
info(c) | ||
info(s) | ||
info(r) | ||
} |