Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iteration chapter improvement #769

Open
AptRoApt opened this issue Jun 2, 2024 · 2 comments
Open

Iteration chapter improvement #769

AptRoApt opened this issue Jun 2, 2024 · 2 comments

Comments

@AptRoApt
Copy link

AptRoApt commented Jun 2, 2024

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:
    const repeatCount = 5
    
    func Repeat(character string) string {
       var sb strings.Builder // do not forget to import "strings"
       for i := 0; i < repeatCount; i++ {
           sb.WriteString(character)
       }
       return sb.String()
    }
  • Launch becnhmarking after refactoring

I can make a pull request with this changes, if you want.

@AptRoApt
Copy link
Author

AptRoApt commented Jun 2, 2024

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.

@quii
Copy link
Owner

quii commented Jun 3, 2024

Great idea! Go for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants