Created
April 18, 2014 04:55
-
-
Save roongr2k7/11025433 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" | |
type Areaer interface { | |
Area() float64 | |
} | |
type Shape struct { | |
Areaer | |
} | |
type Circle struct { | |
radius float64 | |
} | |
func (c Circle) Area() float64 { | |
return 3.14 * (c.radius * c.radius) | |
} | |
type Rectangle struct { | |
width, length float64 | |
} | |
func (r Rectangle) Area() float64 { | |
return r.width * r.length | |
} | |
func (s Shape) String() string { | |
return fmt.Sprintf("[%T] area = %f", s.Areaer, s.Area()) | |
} | |
func main() { | |
r := Shape{Rectangle{3, 4}} | |
c := Shape{Circle{2}} | |
fmt.Println(r) | |
fmt.Println(c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment