Ian Lance Taylor wrote:
> On Sat, Jul 9, 2016 at 4:38 PM, Erich Rickheit KSC <rickh...@numachi.com> 
> wrote:
> > I found myself writing code like this:
> >
> >         s := make([]byte, len)
> >         for i := 0; i < len; i++ {
> >                 // fill in s with stringy goodness
> >         }
> >         return string(s)
> >
> > Does this reuse the memory in s for the string, or does it allocate new
> > memory and copy? Or does escape analysis swoop in and make that decision?
> 
> This will normally allocate new memory for the string and copy over
> the bytes.  I believe that the compiler could optimize this case, but
> as far as I know no Go compiler currently implements that
> optimization.
> 
> Ian

So, the lesson is, try to use []byte or []rune when I need manipulate text.

In those cases where I do need to build actual string objects, are there
ways to get the compiler to not do extra copies? For example, if I write

        str1 := "foo" + str2 + "bar" + secretStringConstant

Does it know to build one string, or does it build the intermediates?
How about:

        str1 := "foo"
        str1 += str2
        str1 += "bar"
        str1 += secretStringConstant

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to