From 917ce1638f4209551dc8e933e74d3bdb1338cf67 Mon Sep 17 00:00:00 2001 From: vinchent Date: Tue, 10 Sep 2024 13:20:33 +0200 Subject: [PATCH] iteration: add simple iteration function --- iteration/iter.go | 12 ++++++++++++ iteration/iter_test.go | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 iteration/iter.go create mode 100644 iteration/iter_test.go diff --git a/iteration/iter.go b/iteration/iter.go new file mode 100644 index 0000000..1de0fc3 --- /dev/null +++ b/iteration/iter.go @@ -0,0 +1,12 @@ +package iteration + +const repeatTime = 5 + +// Repeat takes a string and repeat it 5 times +func Repeat(s string) string { + var res string + for i := 0; i < repeatTime; i++ { + res += s + } + return res +} diff --git a/iteration/iter_test.go b/iteration/iter_test.go new file mode 100644 index 0000000..fd7d5e6 --- /dev/null +++ b/iteration/iter_test.go @@ -0,0 +1,12 @@ +package iteration + +import "testing" + +func TestRepeat(t *testing.T) { + repeated := Repeat("a") + expected := "aaaaa" + + if repeated != expected { + t.Errorf("expected %q but got %q", expected, repeated) + } +}