-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Learned about how loops work in go, how benchmarks
work and also practice my knowledge of Examples
- Loading branch information
1 parent
31ee7bb
commit cc6f6e6
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |