From cf47b70aae4ffe44893d93a5c715a7819b5f7520 Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 2 Mar 2026 13:20:57 +0100 Subject: [PATCH] feat: methods --- go-by-example/methods/methods.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 go-by-example/methods/methods.go diff --git a/go-by-example/methods/methods.go b/go-by-example/methods/methods.go new file mode 100644 index 0000000..0b245b9 --- /dev/null +++ b/go-by-example/methods/methods.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +type rect struct { + width, height int +} + +func (r *rect) area() int { + return r.width * r.height +} + +func (r rect) perim() int { + return 2*r.width + 2*r.height +} + +func main() { + r := rect{width: 10, height: 5} + + fmt.Println("area: ", r.area()) + fmt.Println("preim: ", r.perim()) + + rp := &r + fmt.Println("area: ", rp.area()) + fmt.Println("perim: ", rp.perim()) +}