Skip to content

Instantly share code, notes, and snippets.

@jay-babu
Created October 4, 2022 01:57
Show Gist options
  • Save jay-babu/579d495a3595378e71313d29ab1e4d3d to your computer and use it in GitHub Desktop.
Save jay-babu/579d495a3595378e71313d29ab1e4d3d to your computer and use it in GitHub Desktop.
SOLID: Open-Closed Principle `bad`
package main
import (
"fmt"
"math"
)
type badRect struct {
Width, Height float64
}
type badCircle struct {
Radius float64
}
// Must be changed for every new struct.
func badMeasureArea(i any) {
fmt.Println("calculating area for: ")
switch i.(type) {
case badRect:
r := i.(badRect)
fmt.Println("Rectangle")
fmt.Println(r.Width * r.Height)
case badCircle:
c := i.(badCircle)
fmt.Println("Circle")
fmt.Println(math.Pi * c.Radius * c.Radius)
}
fmt.Println()
}
// ---
func main() {
r := badRect{Width: 3, Height: 4}
c := badCircle{Radius: 5}
badMeasureArea(r)
badMeasureArea(c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment