Re: [go-nuts] Assigning byte to string location is not allowed

2020-08-13 Thread Kevin Chadwick
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

Re: [go-nuts] Assigning byte to string location is not allowed

2020-08-13 Thread Brian Candler
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.

Re: [go-nuts] Assigning byte to string location is not allowed

2020-08-13 Thread Sathish VJ
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?

Re: [go-nuts] Assigning byte to string location is not allowed

2020-08-13 Thread Jan Mercl
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.

Re: [go-nuts] Assigning byte to string location is not allowed

2020-08-13 Thread Harald Weidner
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

[go-nuts] Assigning byte to string location is not allowed

2020-08-13 Thread Sathish VJ
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