Skip to content

Instantly share code, notes, and snippets.

@roshnet
Created April 9, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roshnet/39311733982660ed7bd823436ec00e2b to your computer and use it in GitHub Desktop.
Save roshnet/39311733982660ed7bd823436ec00e2b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
// =======================
// The structs' definition
// =======================
type rectangle struct {
width float64
length float64
}
type circle struct {
radius float64
}
// ========================
// The interface definition
// ========================
type Shape interface {
findPerimeter() float64
findArea() float64
}
// ==================================================
// Receiver functions (as specified by the interface)
// ==================================================
// Receiver function for area of circle
func (c circle) findArea() float64 {
return math.Pi * c.radius * c.radius
}
// Receiver function for area of rectangle
func (r rectangle) findArea() float64 {
return r.length * r.width
}
// Receiver function for perimeter of circle
func (c circle) findPerimeter() float64 {
return 2 * math.Pi * c.radius
}
// Receiver function for perimeter of rectangle
func (r rectangle) findPerimeter() float64 {
return 2 * (r.length + r.width)
}
// ============================================
// The function which brings all the difference
// ============================================
func getProperty(s Shape) {
fmt.Println("The area is", s.findArea())
fmt.Println("The perimeter is", s.findPerimeter())
}
// ===================
// The main() function
// ===================
func main() {
rect := rectangle{
width: 10.7,
length: 20.3,
}
cir := circle{
radius: 4,
}
fmt.Println("RECTANGLE:")
getProperty(rect)
fmt.Println("\nCIRCLE:")
getProperty(cir)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment