Created
October 4, 2022 01:57
-
-
Save jay-babu/579d495a3595378e71313d29ab1e4d3d to your computer and use it in GitHub Desktop.
SOLID: Open-Closed Principle `bad`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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