“This is the 22nd day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

preface

This article mainly introduces the data structure of string storage and how to implement string concatenation in GO language.

String data structure

type StringHeader struct {
   Data uintptr
   Len  int
}
Copy the code

Note that the Data field is not sufficient to ensure that the Data it references will not be garbage collected, so the program must keep a separate, correctly typed pointer to the underlying Data.

The go developers didn’t think it was a good idea to use characters to represent the elements of a string, so they used tune instead, which can be read as INT32.

Concatenation of strings

Strings are divided into constants and variables: concatenation of constants occurs at compile time, while concatenation of string variables occurs at run time. The GO language uses different memory allocation strategies depending on string size. When the concatenated S string is less than 32 bytes, a temporary cache is used for it. When the concatenated string is larger than 32 bytes, memory allocation in the heap is requested.

Constant splicing

  • The noder.sum function is called to concatenate two string constants: append all string constants into an array and concatenate the data.
  • During the function traversal phase, the Walkexpr function determines which run-time splicing function to use. The walkexpr function calls addstr (n, init). When the number of concatenations is less than or equal to 5, the corresponding functions in runtimes concatString1 to concatString5 are called. When the number of strings is greater than 5, the run-time concatstrings function is called and the string is passed in through slicing.

Variable stitching

Run-time string variable concatenation does not simply merge one string into another, but rather creates a larger memory into which the string is copied by memory copy.