Although interfaces in Go and TS are quite different, it was fun to see a comparison between how these are being used in both contexts.
interface Geometry { area(): number;}
class Rect implements Geometry { constructor(private height: number, private width: number) {} area(): number { return this.height * this.width; }}
class Circle implements Geometry { constructor(private radius: number) {} area(): number { return Math.PI * this.radius * this.radius; }}
class Triangle implements Geometry { constructor(private base: number, private height: number) {} area(): number { return (this.base * this.height) / 2.0; }}
function measure(g: Geometry): void { console.log('area :', g.area());}
const r = new Rect(3, 4);const c = new Circle(5);const t = new Triangle(5, 6);
measure(r);measure(c);measure(t);
package main
import ( "fmt" "math")
type geometry interface { area() float64}
type rect struct { width, height float64}
type circle struct { radius float64}
type triangle struct { base, height float64}
func (r rect) area() float64 { return r.width * r.height}
func (c circle) area() float64 { return math.Pi * c.radius * c.radius}
func (t triangle) area() float64 { return (t.base * t.height)/2}
func measure(g geometry) { fmt.Println(g) fmt.Println("area :", g.area())}
func main() { r := rect{width: 3, height: 4} c := circle{radius: 5} t := triangle{base:5, height: 6}
measure(r) measure(c) measure(t)}