Skip to content

strings: Copying Builders allows string mutation #23084

Description

@fweimer

This program:

package main

import (
        "fmt"
        "strings"
)

func main() {
        var b1 strings.Builder
        b1.Grow(3)
        b2 := b1
        b1.WriteString("foo")
        s := b1.String()
        fmt.Printf("string before patching: %#v\n", s)
        b2.WriteString("bar")
        fmt.Printf("string after patching: %#v\n", s)
}

Prints:

string before patching: "foo"
string after patching: "bar"

I think another level of indirection is needed in the implementation, so that the internal buffer is shared even after a copy has been made.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions