Skip to content

Instantly share code, notes, and snippets.

@roongr2k7
Created April 18, 2014 04:55
Show Gist options
  • Save roongr2k7/11025433 to your computer and use it in GitHub Desktop.
Save roongr2k7/11025433 to your computer and use it in GitHub Desktop.
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