Under-optimized string concatenation

Mistake

This allocates a new string for each loop. This is due to a string being immutable in Go.

func concat(values []string) string {
  s := ""
  for _, value := range values {
    s += value
  }
  return s
}

Fix

func concat(values []string) string {
	total := 0
	// Get total number of bytes
	for i := 0; i < len(values); i++ {
		total += len(values[i])
	}

	sb := strings.Builder{}
	sb.Grow(total)
	for _, value := range values {
		_, _ = sb.WriteString(value)
	}

	return sb.String()
}

References