# Using interface types as map keys

**URL:** <https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706>\
**Category:** Technical Discussion\
**Created:** [September 29, 2015, 10:44pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706 "2015-09-29T22:44:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![cespare](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cespare/32/831_2.png) [@cespare](https://forum.golangbridge.org/u/cespare)\
**Post date:** [September 29, 2015, 10:44pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/1 "2015-09-29T22:44:53Z")

</div>

(I started a conversation on Twitter but @adg suggested it would be a good time to try out this forum.)

@adg has advised in the past that it isn’t a good idea to use interface types as map keys because if an implementation is provided which has an incomparable type (map, slice, func), it will result in a runtime panic.

[https://talks.golang.org/2015/tricks.slide#25](https://talks.golang.org/2015/tricks.slide#25) (and also Docker pull request 15751; can’t post the link because this forum has an annoying link limit for new users)

This is of course sensible and I agree in general, but I’ve also found cases where I’m not easily able to avoid using an interface in a map key. The one that comes to mind at the moment is when making use of http.ConnState callback. Sometimes I’ve used that to get statistics on the number of connections in various states, and in that case I’ve used `c.RemoteAddr()` as my map key. But at other times, it’s more natural to use `net.Conn` itself as the key. See, for example, @bradfitz’s recent CL for net/http/httptest:

[https://golang.org/cl/15151](https://golang.org/cl/15151)

where he uses `map[net.Conn]bool` to represent a set of `net.Conn`s.

One way to work around this would be to use a `map[string]net.Conn` instead of a `map[net.Conn]bool` and then use `c.RemoteAddr()` as the key.

Is this kind of defensive programming warranted, though?

---

<div class="post-metadata">

**Author:** ![Unknwon](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/unknwon/32/2782_2.png) [@Unknwon](https://forum.golangbridge.org/u/Unknwon)\
**Post date:** [September 29, 2015, 10:54pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/2 "2015-09-29T22:54:20Z")

</div>

> [@cespare](#):
>
> @adg has advised in the past that it isn’t a good idea to use interface types as map keys because if an implementation is provided which has an incomparable type (map, slice, func), it will result in a runtime panic.

In my point of view, there is always a chance you have runtime panic even if you’re writing code that you feel most comfortable with. Also, things can be worked around to reduce the probabilities of runtime panic such as do a type check before using it to access value in the map.

So… do most comfortable way for yourself.

---

<div class="post-metadata">

**Author:** ![cespare](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cespare/32/831_2.png) [@cespare](https://forum.golangbridge.org/u/cespare)\
**Post date:** [September 29, 2015, 11:05pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/3 "2015-09-29T23:05:58Z")

</div>

> In my point of view, there is always a chance you have runtime panic even if you’re writing code that you feel most comfortable with.

In most of the code I write, I strive to make it so that runtime panics only occur if I (the programmer) have made a mistake.

> Also, things can be worked around to reduce the probabilities of runtime panic such as do a type check before using it to access value in the map.

In this case, the concrete type that it’s going to be 99.9% of the time is the unexported type `net.conn`, so that doesn’t apply.

If I’m going to work around it, I think it’s simpler to avoid using the interface type as the map key in the first place.

---

<div class="post-metadata">

**Author:** ![Unknwon](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/unknwon/32/2782_2.png) [@Unknwon](https://forum.golangbridge.org/u/Unknwon)\
**Post date:** [September 29, 2015, 11:10pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/4 "2015-09-29T23:10:28Z")

</div>

> [@cespare](#):
>
> In most of the code I write, I strive to make it so that runtime panics only occur if I (the programmer) have made a mistake.

If you use third-party libraries, there will be mistakes.

> In this case, the concrete type that it’s going to be 99.9% of the time is the unexported type net.conn, so that doesn’t apply.

Then the case condition go to you don’t want to work around.

And what do you mean by `unexported type net.conn` exactly? Didn’t `net.Conn` is exported?

---

<div class="post-metadata">

**Author:** ![bradfitz](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/bradfitz/32/349_2.png) [@bradfitz](https://forum.golangbridge.org/u/bradfitz)\
**Post date:** [September 29, 2015, 11:16pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/5 "2015-09-29T23:16:26Z")

</div>

I considered that problem when I wrote that code but I realized in this case, all net.Conn cases are from net.Listener (newLocalListener in that file), so it’s safe.

---

<div class="post-metadata">

**Author:** ![cespare](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cespare/32/831_2.png) [@cespare](https://forum.golangbridge.org/u/cespare)\
**Post date:** [September 29, 2015, 11:18pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/6 "2015-09-29T23:18:26Z")

</div>

The problem is: you have a `net.Conn` that you want to use as a map key. If it’s a `*net.conn`, then that’s safe; if it’s some other concrete type, then it might not be.

1. How do you check whether the `net.Conn` is a `*net.conn`?
2. What do you do if it isn’t?

I’m claiming that if you care about protecting yourself from this problem, the solution is to not use a `net.Conn` as a map key at all.

My question is: should I care?

---

<div class="post-metadata">

**Author:** ![Unknwon](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/unknwon/32/2782_2.png) [@Unknwon](https://forum.golangbridge.org/u/Unknwon)\
**Post date:** [September 29, 2015, 11:19pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/7 "2015-09-29T23:19:50Z")

</div>

> [@cespare](#):
>
> How do you check whether the net.Conn is a \*net.conn?

Package `reflect` can tell you if it’s a pointer or not.

> What do you do if it isn’t?

If it’s not exported, how can you access it to use as key type of map?

---

<div class="post-metadata">

**Author:** ![cespare](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cespare/32/831_2.png) [@cespare](https://forum.golangbridge.org/u/cespare)\
**Post date:** [September 29, 2015, 11:21pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/8 "2015-09-29T23:21:52Z")

</div>

> Package reflect can tell you if it’s a pointer or not.

This is very much more work/trouble than not using a `net.Conn` as a map key.

> If it’s not exported, how can you access it to use as key type of map?

The question is about using `net.Conn` as a map key.

---

<div class="post-metadata">

**Author:** ![dskinner](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dskinner/32/746_2.png) [@dskinner](https://forum.golangbridge.org/u/dskinner)\
**Post date:** [September 29, 2015, 11:22pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/9 "2015-09-29T23:22:37Z")

</div>

This is really a matter of context. Who will be using the map? How will the map be accessible? What might you be using as a key (concrete types)? Where are those types coming from (are you writing them, 3rd party lib)? Who else will be working with this code (will they recognize the same concerns when making changes)?

If the surface area is really small, I wouldn’t fret over it. Otherwise, why risk it.

---

<div class="post-metadata">

**Author:** ![Unknwon](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/unknwon/32/2782_2.png) [@Unknwon](https://forum.golangbridge.org/u/Unknwon)\
**Post date:** [September 29, 2015, 11:23pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/10 "2015-09-29T23:23:39Z")

</div>

> This is very much more work/trouble than not using a net.Conn as a map key.

If you get too many trouble with it, I don’t see any point that why you’re arguing/wondering using it or not.

> The question is about using net.Conn as a map key.

I’m completely confused by what have you said in the post… 😓

---

<div class="post-metadata">

**Author:** ![cespare](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cespare/32/831_2.png) [@cespare](https://forum.golangbridge.org/u/cespare)\
**Post date:** [September 29, 2015, 11:24pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/11 "2015-09-29T23:24:06Z")

</div>

> I’m completely confused by what have you said in the post…

Likewise 😛

---

<div class="post-metadata">

**Author:** ![cespare](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cespare/32/831_2.png) [@cespare](https://forum.golangbridge.org/u/cespare)\
**Post date:** [September 29, 2015, 11:29pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/12 "2015-09-29T23:29:19Z")

</div>

Thanks Brad! I don’t quite understand/agree, but we can keep that discussion on the CL.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [December 28, 2015, 11:29pm UTC](https://forum.golangbridge.org/t/using-interface-types-as-map-keys/706/13 "2015-12-28T23:29:19Z")

</div>

Closed on December 28, 2015, 11:29pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
