Add star-shapes-mk2 exercise

Now improved using methods and interfaces!
This commit is contained in:
raul 2024-04-17 10:07:09 +02:00
parent fb05399eca
commit 725071ded5
2 changed files with 50 additions and 0 deletions

3
star-shapes-mk2/go.mod Normal file
View File

@ -0,0 +1,3 @@
module star-shapes-mk2
go 1.22.2

47
star-shapes-mk2/main.go Normal file
View File

@ -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())
}