You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Strings concatenation is a great topic to learn through TDD, but this topic is not covered in this chapter.
Strings in Go are immutable, so memory reallocates on every change of a string. We can demonstrate this with an example by benchmarking and show a solution: strings.Builder from the strings package.
To do this we can:
Add benchmarking before the refactoring stage and run it.
Provide a short explanation about strings in Go, explaining the purpose of using strings.Builder.
Add strings.Builder in the refactor stage. The final function will look like:
constrepeatCount=5funcRepeat(characterstring) string {
varsb strings.Builder// do not forget to import "strings"fori:=0; i<repeatCount; i++ {
sb.WriteString(character)
}
returnsb.String()
}
Launch becnhmarking after refactoring
I can make a pull request with this changes, if you want.
The text was updated successfully, but these errors were encountered:
I think that work with strings can be discussed after the chapter on arrays and slices. This section can cover not only strings.Builder, but also runes, bytes, and work with UTF-8.
Strings concatenation is a great topic to learn through TDD, but this topic is not covered in this chapter.
Strings in Go are immutable, so memory reallocates on every change of a string. We can demonstrate this with an example by benchmarking and show a solution:
strings.Builder
from thestrings
package.To do this we can:
strings.Builder
.strings.Builder
in the refactor stage. The final function will look like:I can make a pull request with this changes, if you want.
The text was updated successfully, but these errors were encountered: