Created
April 9, 2020 15:25
-
-
Save roshnet/93a2596a51ce55e81abc896cb74a226a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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