Skip to content

Commit

Permalink
Learned about how loops work in go, how benchmarks
Browse files Browse the repository at this point in the history
work and also practice my knowledge of Examples
  • Loading branch information
vedicpanda committed May 25, 2023
1 parent 31ee7bb commit cc6f6e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Iteration/repeat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package iteration

func Repeat(character string, repeatCount int) (repeated string) {
for i := 0; i < repeatCount; i++ {
repeated += character
}
return
}
29 changes: 29 additions & 0 deletions Iteration/repeat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package iteration

import (
"fmt"
"testing"
)

func Test_Repeater(t *testing.T) {
repeated := Repeat("a", 5)
expected := "aaaaa"
if repeated != expected {
t.Errorf("expected %q but got %q", expected, repeated)
}
}

// go test -bench="." to run benchmarks
// Benchmarks essentially allow us to gauge on average how fast our program runs!
func BenchmarkRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
Repeat("a", 5)
}

}

func ExampleRepeat() {
repeat := Repeat("a", 7)
fmt.Println(repeat)
//Output: aaaaaaa
}

0 comments on commit cc6f6e6

Please sign in to comment.