Skip to content

Instantly share code, notes, and snippets.

@nomkhonwaan
Last active January 2, 2020 03:01
Show Gist options
  • Save nomkhonwaan/c89ce25fafe3ffc5c8c8592cebb77d9e to your computer and use it in GitHub Desktop.
Save nomkhonwaan/c89ce25fafe3ffc5c8c8592cebb77d9e to your computer and use it in GitHub Desktop.
go-solid-open-closed-5dfc2e81a33fd7a8f2eebc5a
package main
import "fmt"
type square struct {
width, height int
}
func main() {
s := square{width: 10, height: 10}
printArea(s)
}
// printArea prints an area number of the shape
func printArea(s square) {
fmt.Printf("the shape area is: %d\n", s.width*s.height)
}
// a shape interface which provides its area calculation method
type shape interface {
area() float64
}
// printArea prints an area number of the shape
func printArea(s shape) {
fmt.Printf("the shape area is: %.2f\n", s.area())
}
// printArea prints an area number of the shape
func printArea(variousShape interface{}) {
switch s := variousShape.(type) {
case square:
fmt.Printf("the shape area is: %d\n", s.width*s.height)
case rectangle:
fmt.Printf("the shape area is: %d\n", s.width*s.height)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment