Created
February 2, 2022 09:41
-
-
Save ihsanbudiman/7e5ef632c262491a40141328b96b7414 to your computer and use it in GitHub Desktop.
openclosed in go
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
type Persegi struct { | |
Sisi float64 | |
} | |
type Segitiga struct { | |
Alas float64 | |
Tinggi float64 | |
} | |
type Kalkulator struct{} | |
func (k Kalkulator) TotalLuas(daftarBenda ...interface{}) float64 { | |
totalLuas := 0.0 | |
for _, benda := range daftarBenda { | |
switch benda.(type) { | |
case Segitiga: | |
alas := benda.(Segitiga).Alas | |
tinggi := benda.(Segitiga).Tinggi | |
totalLuas += (alas * tinggi) / 2 | |
case Persegi: | |
sisi := benda.(Persegi).Sisi | |
totalLuas += sisi * sisi | |
} | |
} | |
return totalLuas | |
} | |
func main() { | |
p := Persegi{4} | |
s := Segitiga{3, 4} | |
k := Kalkulator{} | |
fmt.Println(k.TotalLuas(p, s)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment