Re: [go-nuts] Regarding string immutablity

2018-08-28 Thread Jan Mercl
On Tue, Aug 28, 2018 at 4:55 PM Jay Sharma  wrote:

> As I am overwriting the content with new content, Is it inplace
replacement [same memory is being updated] or a new copy will be created
with new content and old content will be there in the memory ?

Basically none of that. Strings indeed are immutable. Consider

   s := "foo"
   t := s
   s := "bar"

After this t == "foo" is still true. The implementation of this is that a
mutable _variable_ of type string internally points to an immutable string
_value_ (in the first approximation). So assigning(copying) the 's'
variable makes 't' have the value "foo" and keeps it even if the variable
's' is later mutated to point to a different string value. The concept
extends to any RHS string expression assignment, not only 's'. The LHS (or
the pointee in p :=  *pt := whatever) is mutable independently of the
immutable value it represents (provides, points to).

The nice side effect is that every string variable is unique, but the
string values they represent may be shared without copying the actual
string value - only the string variable is ever copied. And that's an O(1)
thing.

-- 

-j

-- 
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.


[go-nuts] Regarding string immutablity

2018-08-28 Thread Jay Sharma
Hi All,

I went through documentation and many post. Every where it is specified 
*strings 
are immutable*.

I have some string :

x := "teststring"


I want to *wipe out/overwrite* the content of this string x from 
disk/memory. 

As per me the simplest way to do this:

x = ""


or If I want to overwrite it with some new content, I can do this: [*Here 
length of new string is equal to length of old string*]

x = "new1string"


As I am overwriting the content with new content, Is it *inplace 
replacement* [same memory is being updated] or a new copy will be created 
with new content and old content will be there in the memory ? 


-- 
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.