Skip to content

Commit

Permalink
Merge pull request #2778 from arcAman07/master
Browse files Browse the repository at this point in the history
Calculating area of shapes using Interfaces in go.
  • Loading branch information
fineanmol authored Oct 7, 2022
2 parents 2e6bdaf + 83b4073 commit c98ead2
Showing 1 changed file with 37 additions and 0 deletions.
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)
}

0 comments on commit c98ead2

Please sign in to comment.