On 2020-08-13 14:16, Brian Candler wrote:
>
> If you want a mutable string, look at []byte instead, and read the excellent
> intro to slices at https://blog.golang.org/slices
>
Also worth remembering that unicode strings aren't necessarily bytes, so
requiring verbose action for byte access, inst
Immutable strings are a useful language feature. For one thing, it makes
them usable as map keys, without having to duplicate the entire contents.
A copy-by-value is cheap (it copies only the pointer and length):
b := a
... but you know that there are never any issues around aliasing, e.g.
Why would the design not allow the contents of the string to be changed? I
understand that the size and memory allocation for it is immutable.
On Thursday, 13 August 2020 at 18:50:10 UTC+5:30 Jan Mercl wrote:
> On Thu, Aug 13, 2020 at 3:02 PM Sathish VJ wrote:
>
> > Why is this not allowed?
On Thu, Aug 13, 2020 at 3:02 PM Sathish VJ wrote:
> Why is this not allowed?
>
> s := "hello"
> s[0] = 'a' // compile error: cannot assign to s[0]
>
> https://play.golang.org/p/zyJXwhEeKPo
Strings are immutable: once created, it is impossible to change the
contents of a string.
Hello,
> Why is this not allowed?
>
> s := "hello"
> s[0] = 'a' // compile error: cannot assign to s[0]
Because strings are immutable in Go. See
https://golang.org/ref/spec#String_types
You can construct a new string, or convert to byte slive and back.
https://play.golang.org/p/0LvsxCC
Why is this not allowed?
s := "hello"
s[0] = 'a' // compile error: cannot assign to s[0]
https://play.golang.org/p/zyJXwhEeKPo
--
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