Last active
January 2, 2020 03:01
-
-
Save nomkhonwaan/c89ce25fafe3ffc5c8c8592cebb77d9e to your computer and use it in GitHub Desktop.
go-solid-open-closed-5dfc2e81a33fd7a8f2eebc5a
This file contains hidden or 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" | |
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) | |
} |
This file contains hidden or 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
// 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()) | |
} |
This file contains hidden or 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
// 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