Go question about HTTP Header

Gustavo Niemeyer gustavo.niemeyer at canonical.com
Tue Dec 11 15:32:47 UTC 2012


On Tue, Dec 11, 2012 at 1:16 PM, roger peppe <roger.peppe at canonical.com> wrote:
>> I realize that it works, because map is a "pointer" type (the actual
>> map references the underlying storage). So even though
>> texproto.MIMEHeader(h) is actually creating a new map object, they
>> both reference the same real hash map, so adding data to it updates
>> the same storage.
>
> it's not really creating a new map object, any more than
> assigning a map to a new variable is creating a new map object.
> it's converting the value's type, which is allowed because both
> the old and the new type have the same underlying type.

That's correct, but orthogonal. John's statement is pretty much spot
on. There's indeed a new map value being created (that's what I assume
John means by "map object"), and they indeed reference the same
underlying hash map, and the reason is indeed because map values in Go
actually behave like references.

>> type Header {
>>  textproto.MIMEHeader
>> }
(...)
> but it's not so convenient to use, because it's actually
> quite nice to have the underlying type be a map,
> so you do all the usual map operations on it.

Agreed. In addition, Header is the general one, so it seems right from
a design standpoint. In other words, MIMEHeader is just one possible
way to interpret a Header.


gustavo @ http://niemeyer.net



More information about the Juju-dev mailing list