Assigning string values to string values or byte[] to byte[] does not 
require additional memory allocation.
But conversion between string and byte[] does so most of the time, because 
the underlying array of a string is immutable, versus the mutable of a 
byte[].

Encapsulating the type cast encapsulates exactly this potentially costly 
operation.
Whether this has a negativ or positive impact on potential compiler 
optimisation in respect to the need of a copy
in this special case, is hard to determine.

Amnon schrieb am Dienstag, 18. Mai 2021 um 22:09:18 UTC+2:

> My understanding was that a string had a pointer and a length,
> whereas a slice had a pointer and a length and a capacity.
>
> https://golang.org/src/reflect/value.go?s=59515:59567#L1973
>
>
> On Tuesday, 18 May 2021 at 20:32:39 UTC+1 Brian Candler wrote:
>
>> Assigning a string value to another variable doesn't double the memory 
>> usage.
>>
>> A value of type string consists of a pointer, a length, and a capacity, 
>> and only these are copied - so you get another copy of the pointer, 
>> pointing to the same data buffer.
>>
>>>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/188a70df-4dff-40d7-a2d2-4ea64902aa28n%40googlegroups.com.

Reply via email to