Advertisement
JcGNeon

test.go

May 7th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.83 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math"
  6. )
  7.  
  8. type geometry interface {
  9.     area() float64
  10.     perim() float64
  11. }
  12.  
  13. type rect struct {
  14.     width, height float64
  15. }
  16.  
  17. type circle struct {
  18.     radius float64
  19. }
  20.  
  21. func(r rect) area() float64 {
  22.     return r.width * r.height
  23. }
  24.  
  25. func(r rect) perim() float64 {
  26.     return 2*r.width + 2*r.height
  27. }
  28.  
  29. func(c circle) area() float64 {
  30.     return math.Pi * (c.radius * c.radius)
  31. }
  32.  
  33. func(c circle) perim() float64 {
  34.     return 2 * math.Pi * c.radius
  35. }
  36.  
  37. func printStuff(g geometry) {
  38.     fmt.Println(g)
  39.     fmt.Println("Area:", g.area())
  40.     fmt.Println("Perim:", g.perim())
  41. }
  42.  
  43. func main() {
  44.     r := rect{width: 5, height: 10}
  45.     c := circle{radius: 6}
  46.  
  47.     printStuff(r)
  48.     printStuff(c)
  49.  
  50.     fmt.Println("Press ENTER to exit the program.")
  51.     fmt.Scanln()
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement