Created
June 24, 2019 18:47
-
-
Save victorsteven/bfbe3c4eced42bdb08dca54423702314 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" | |
| ) | |
| type square struct { | |
| length float64 | |
| } | |
| type circle struct { | |
| radius float64 | |
| } | |
| type shape interface { | |
| area() float64 | |
| } | |
| func (s square) area() float64 { | |
| return s.length * s.length | |
| } | |
| func (c circle) area() float64 { | |
| return math.Pi * c.radius * c.radius | |
| } | |
| func calArea(s shape) { | |
| switch s.(type) { | |
| case square: | |
| fmt.Println("This is the area of the square: ", s.area()) | |
| case circle: | |
| fmt.Println("This is the area of the circle: ", s.area()) | |
| } | |
| } | |
| func main() { | |
| c := circle{ | |
| radius: 2.5, | |
| } | |
| s := square{ | |
| length: 1.5, | |
| } | |
| calArea(c) | |
| calArea(s) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment