RSS Amplifier

spacetime.dev

mutable strings in go

0
Sign in to vote or save

This site does not allow itself to be embedded. You can still read it on the original site — the toolbar below keeps your place in the directory.

According to the Go language specification : Strings are immutable: once created, it is impossible to change the contents of a string. We’ll see about that. data := [] byte ( "yellow submarine" ) str := * ( * string )( unsafe . Pointer ( & data )) for i := range data { data [ i ] = 'x' fmt . Printf ( "%s\n%s\n\n" , string ( data ), str ) } Try it out yourself .

According to the Go language specification:

Strings are immutable: once created, it is impossible to change the contents of a string.

We’ll see about that.

data := []byte("yellow submarine")
str := *(*string)(unsafe.Pointer(&data))

for i := range data {
    data[i] = 'x'
    fmt.Printf("%s\n%s\n\n", string(data), str)
}

Try it out yourself.

Read on spacetime.dev

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.