From 725071ded53e996e974bff93286c48a898848535 Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 17 Apr 2024 10:07:09 +0200 Subject: [PATCH] Add star-shapes-mk2 exercise Now improved using methods and interfaces! --- star-shapes-mk2/go.mod | 3 +++ star-shapes-mk2/main.go | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 star-shapes-mk2/go.mod create mode 100644 star-shapes-mk2/main.go diff --git a/star-shapes-mk2/go.mod b/star-shapes-mk2/go.mod new file mode 100644 index 0000000..ccfa1fe --- /dev/null +++ b/star-shapes-mk2/go.mod @@ -0,0 +1,3 @@ +module star-shapes-mk2 + +go 1.22.2 diff --git a/star-shapes-mk2/main.go b/star-shapes-mk2/main.go new file mode 100644 index 0000000..2e3d016 --- /dev/null +++ b/star-shapes-mk2/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +type Shaper interface { + Shape(int) Square +} + +type Square struct { + form string + quantity int +} + +func (s Square) Shape(num int) Square { + for i := 0; i < num; i++ { + for i := 0; i < num; i++ { + s.form += "* " + } + s.form += "\n" + } + + s.quantity = num + return s +} + +func showShape(s Shaper, num int) { + fmt.Println(s.Shape(num).form) +} + +func getNumberStars() int { + numStr := os.Args[1] + num, err := strconv.Atoi(numStr) + if err != nil { + log.Panicln(err) + } + return num +} + +func main() { + s := Square{} + showShape(s, getNumberStars()) +}