Skip to content

Instantly share code, notes, and snippets.

@soh-i
Created January 30, 2014 16:00
Show Gist options
  • Save soh-i/8711854 to your computer and use it in GitHub Desktop.
Save soh-i/8711854 to your computer and use it in GitHub Desktop.
Getting started with OOP for go-lang
Area of r1 is: 24.000000
Area of r2 is: 2079.000000
Area of c1 is: 1134.114948
Area of c2 is: 1385.442360
package main
import (
. "fmt"
"math"
)
type Rectangle struct {
width float64
height float64
}
type Circle struct {
radius float64
}
func (r Rectangle) area() float64 {
return r.width * r.height
}
func (c Circle) area() float64 {
return c.radius * c.radius * math.Pi
}
func main() {
r1 := Rectangle{12, 2}
r2 := Rectangle{21, 99}
c1 := Circle{19}
c2 := Circle{21}
Printf("Area of r1 is: %f\n", r1.area())
Printf("Area of r2 is: %f\n", r2.area())
Printf("Area of c1 is: %f\n", c1.area())
Printf("Area of c2 is: %f\n", c2.area())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment