Skip to content

Instantly share code, notes, and snippets.

@ynwd
Created January 31, 2022 14:34
Show Gist options
  • Save ynwd/36f67391d3ce11409cb7b66e464d82ec to your computer and use it in GitHub Desktop.
Save ynwd/36f67391d3ce11409cb7b66e464d82ec to your computer and use it in GitHub Desktop.
SOLID: single responsibility principle di golang
package main
import (
"fmt"
"golang.org/x/text/currency"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
)
func hitungHargaTanah(harga, panjang, lebar int) string {
// hitung luas
luas := panjang * lebar
// hitung harga total
totalPrice := harga * luas
// format mata uang
cur := currency.MustParseISO("IDR")
scale, _ := currency.Cash.Rounding(cur)
dec := number.Decimal(totalPrice, number.Scale(scale))
p := message.NewPrinter(language.Indonesian)
return p.Sprintf("%v%v", currency.Symbol(cur), dec)
}
func main() {
price := hitungHargaTanah(100, 10, 10)
fmt.Println(price)
}
package main
import (
"fmt"
"golang.org/x/text/currency"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
)
type luas struct{}
func (l *luas) hitungLuasTanah(panjang, lebar int) int {
return panjang * lebar
}
type harga struct{}
func (h *harga) hitungHargaTanah(harga, luas int) int {
return harga * luas
}
type text struct{}
func (t *text) string(harga int) string {
cur := currency.MustParseISO("IDR")
scale, _ := currency.Cash.Rounding(cur)
dec := number.Decimal(harga, number.Scale(scale))
p := message.NewPrinter(language.Indonesian)
return p.Sprintf("%v%v", currency.Symbol(cur), dec)
}
func main() {
l := luas{}
luasTanah := l.hitungLuasTanah(100, 10)
h := harga{}
hargaTanah := h.hitungHargaTanah(100, luasTanah)
t := text{}
fmt.Println(t.string(hargaTanah))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment