Skip to content

Instantly share code, notes, and snippets.

@ynwd
Created January 31, 2022 15:15
Show Gist options
  • Save ynwd/f0182ad2e564d410bd344e1b78555996 to your computer and use it in GitHub Desktop.
Save ynwd/f0182ad2e564d410bd344e1b78555996 to your computer and use it in GitHub Desktop.
SOLID: prinsip liskov
package main
import "fmt"
type A struct{}
func (a A) Test() {
fmt.Println("Printing A")
}
type B struct {
A
}
func ImpossibleLiskovSubstitution(a A) {
a.Test()
}
func main() {
a := B{}
// PANIC : cannot use a (type B) as
// type A in argument to
// ImpossibleLiskovSubstitution
ImpossibleLiskovSubstitution(a)
}
package main
import "fmt"
type A struct{}
func (a A) Test() {
fmt.Println("Printing A")
}
type B struct {
A
}
type testing interface {
Test()
}
func PossibleLiskovSubstitution(a testing) {
a.Test()
}
func main() {
a := B{}
PossibleLiskovSubstitution(a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment