Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exploration of String package in Go

Exploration of String package in Go

It is an in-depth discussion about strings and the strings package, some cool methods, and performance.

Olumide Ogundele

January 11, 2020
Tweet

More Decks by Olumide Ogundele

Other Decks in Programming

Transcript

  1. Strings Strings in Go are just a read-only slice of

    bytes. This means that it can be treated as a read-only slice. https://play.golang.org/p/Ne4u1B0YQ6C
  2. Strings package Strings are built from bytes so indexing them

    yields bytes, not characters. UTF-8 is a central part of their design. Source - go blog strings package has the following files builder.go compare.go reader.go replace.go search.go strings.go
  3. Map function returns the copy of a string with modified

    characters according to the mapping function
  4. String Builder A Builder is used to efficiently build a

    string using Write methods. It minimizes memory copying - https://golang.org/pkg/strings/ We can talk about performance here.
  5. This is what happened In StringBuilder 1, in the for

    loop, on each concatenation, a new copy of the string is created and subsequent strings are copied over, character by character this leads to an algorithm complexity of O(n2). In StringBuilder 2, in the for loop, on each concatenation, we are appending the string to a buffer, which is a slight of byte, and return the accumulated string. Copying is greatly reduced.
  6. You can learn a lot about writing good Go code

    just by reading the package documentation and implementations. I’ll encourage us to do more of it when we can. Also do some study on ASCII, Unicode, Encodings esp. UTF-8.