Purpose of pattern in muxEntry

106 views
Skip to first unread message

Amit Saha

unread,
Dec 14, 2020, 8:52:27 PM12/14/20
to golang-nuts
Hi all, the ServerMux struct is currently defined as:

type ServeMux struct {
// other fields
    m map[string]muxEntry
}


The muxEntry is then defined as:

type muxEntry struct {
    h Handler
    pattern string
}

Is there any reason for also storing the pattern in the muxEntry since it *seems* like the lookup only happens using the key in the map of ServeMux?

Thanks for any insights.


Best Regards,
Amit


jake...@gmail.com

unread,
Dec 15, 2020, 1:49:45 PM12/15/20
to golang-nuts
I have no special knowledge of the code, but it looks like the reason is so that ServeMux.es,  which is a []muxEntry, can be searched. See the latter half of `func (mux *ServeMux) match()` for example.

That said, it may be possible to have ServeMux.m be a `map[string]Handler` instead. If so, then my guess is that ServeMux.m and ServeMux.es both use muxEntry just to slightly simplify the code. The assumption would be that the memory cost is negligible. But I could be wrong, and maybe there is a case where  ServeMux.m must be a muxEntry, and not just a Handler.

Amit Saha

unread,
Dec 15, 2020, 6:46:22 PM12/15/20
to jake...@gmail.com, golang-nuts

On 16 Dec 2020, at 5:49 am, jake...@gmail.com <jake...@gmail.com> wrote:

I have no special knowledge of the code, but it looks like the reason is so that ServeMux.es,  which is a []muxEntry, can be searched. See the latter half of `func (mux *ServeMux) match()` for example. 

That said, it may be possible to have ServeMux.m be a `map[string]Handler` instead. If so, then my guess is that ServeMux.m and ServeMux.es both use muxEntry just to slightly simplify the code. The assumption would be that the memory cost is negligible. But I could be wrong, and maybe there is a case where  ServeMux.m must be a muxEntry, and not just a Handler.

Thank you for sharing your insights. Yes, I was looking at the append..() code slightly below and I saw a usage of that there. 



On Monday, December 14, 2020 at 8:52:27 PM UTC-5 amits...@gmail.com wrote:
Hi all, the ServerMux struct is currently defined as:

type ServeMux struct {
// other fields
    m map[string]muxEntry
}


The muxEntry is then defined as:

type muxEntry struct { 
    h Handler 
    pattern string 
}

Is there any reason for also storing the pattern in the muxEntry since it *seems* like the lookup only happens using the key in the map of ServeMux?

Thanks for any insights.


Best Regards,
Amit



-- 
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/42544681-9f1f-4e76-a4da-52330ee1628en%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages