Conceptually, the value of a string is the sequence of bytes it contains,
just like an array of bytes. It doesn't matter that the implementation of
strings looks more like a slice internally. On the other hand the value of
a slice is a reference to (part of) an underlying array, together with a
length. In all cases == should compare values. I agree that it would be
confusing to make == work for slices, because many people, myself included
would intuitively want that to compare the contents of the slice (values
from the underlying array) rather than the value of the slice itself which
is essentially 3 pointers.

One nice feature of the current gc compiler AIUI is that it will avoid a
copy in some circumstances when you convert a byte slice to a string and
immediately use it as a map key. This helps with using []byte values to
index into maps, but doesn't help when the []byte is part of a larger
structure you want to use as a key.

On Fri, Jul 1, 2016 at 7:05 AM Chad <send2b...@gmail.com> wrote:

>
>
> On Friday, July 1, 2016 at 4:01:54 PM UTC+2, Chad wrote:
>
>>
>>
>> On Friday, July 1, 2016 at 3:44:10 PM UTC+2, Martin Geisler wrote:
>>>
>>> On Fri, Jul 1, 2016 at 12:52 PM, Chad <send...@gmail.com> wrote:
>>> > However, that it's a valid point you raise (re strings)
>>> > But that's exactly the reason for which, someone would probably ask
>>> whether
>>> > a string is a reference type or a value type.
>>> >
>>> > This could probably made clearer in the documentation.
>>>
>>> I keep seeing references (hah!) to this concept of a "reference type"
>>> :-) However, I just tried searching the language spec and Effective Go
>>> and there doesn't seem to be such a concept defined in those
>>> documents.
>>
>>
>> I think it should. It is mentioned here however
>> https://blog.golang.org/go-maps-in-action
>>
>> Without that, one of the first instinct of beginning programmers is often
>> to create pointers to slices ** *or maps ***
>>
>
> (And I should know, I did that :)
>
>
>>
>>> --
> 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.
>

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

Reply via email to