-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strings.Builder example in for's benchmark section (#838)
* Add notes about using and benchmarking strings.Builder * Add new version of for example * Update roman numerals tutorial with backlink * Fix phrasing
- Loading branch information
1 parent
7c2e292
commit 23b2524
Showing
4 changed files
with
65 additions
and
1 deletion.
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,14 @@ | ||
package iteration | ||
|
||
import "strings" | ||
|
||
const repeatCount = 5 | ||
|
||
// Repeat returns character repeated 5 times. | ||
func Repeat(character string) string { | ||
var repeated strings.Builder | ||
for i := 0; i < repeatCount; i++ { | ||
repeated.WriteString(character) | ||
} | ||
return repeated.String() | ||
} |
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,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) | ||
} | ||
} |
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
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