Interfaces
package main
import "fmt"
// decleare a struct called Planet
type Planet struct {
radius float64
gravitation_constant float64
mass float64
}
// define the interface
// in a planetary system, we can calculate the gravitational
// force the planet exerts on a given object with mass
type PlanetSystem interface {
gravitational_force(mass float64) float64
}
// define the interface method
func (p Planet) gravitational_force(object_mass float64) float64 {
F := (p.gravitation_constant * p.mass * object_mass) / (p.radius * p.radius)
return F
}
// make use of the interface
func weight_on_planet(planet PlanetSystem, mass float64) float64 {
return planet.gravitational_force(mass)
}
func main() {
earth := Planet{radius: 6371, gravitation_constant: 6.67e-11, mass: 5.972e24}
fmt.Println(weight_on_planet(earth, 80))
}
Last updated