how to ensure that an interface is comparable to be used in map key?

xuanbao · · 781 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>For example, if a function implements an interface, it cannot be used in map key. I wonder what is the cleanest way to work around that. (A concrete example will be http.HandlerFunc.)</p> <p>my solution is:</p> <pre><code> type handler struct { http.Handler } func NewHandler(h http.Handler) http.Handler { return &amp;handler{Handler: h} } </code></pre> <hr/>**评论:**<br/><br/>trinchan: <pre><p><a href="https://blog.golang.org/go-maps-in-action#TOC_5.">Map keys may be of any type that is comparable. The language spec defines this precisely, but in short, comparable types are boolean, numeric, string, pointer, channel, and interface types, and structs or arrays that contain only those types. Notably absent from the list are slices, maps, and functions; these types cannot be compared using ==, and may not be used as map keys.</a></p> <p>http.HandlerFuncs cannot be map keys because they are functions, but you can certainly use <a href="http://play.golang.org/p/9Zh3cMrrgg">other interfaces, including http.Handler</a> as map keys.</p></pre>djherbis: <pre><p>As a side note, although == isn&#39;t defined for slices and maps its possible to invent your own method for comparing them (such as looping and comparing their elements). You could even create a method to hash slices or maps into a byte array which can be used in a map.</p> <p>However there is currently no nice way to compare functions. Some have suggested using pointer equality, however this is not guaranteed to work in the Go spec, <a href="http://stackoverflow.com/questions/9643205/how-do-i-compare-two-functions-for-pointer-equality-in-the-latest-go-weekly" rel="nofollow">http://stackoverflow.com/questions/9643205/how-do-i-compare-two-functions-for-pointer-equality-in-the-latest-go-weekly</a></p> <p>The spec: <a href="http://weekly.golang.org/doc/go1#equality" rel="nofollow">http://weekly.golang.org/doc/go1#equality</a></p></pre>taylorchu: <pre><p>I now use this solution that makes use of anonymous struct.</p> <p>&amp;struct{ http.Handler } {h}</p></pre>djherbis: <pre><p>Note that this &#34;wrapper&#34; technique will allow you to use incomparable types in map keys however if you wrap the same value twice it will act as two different keys in the map. In this case, the same HandlerFunc could appear many times in the same map&#39;s keys.</p> <p>I ran into this problem when I wanted to create a &#34;set&#34; of functions in Go. My solution was less than satisfying, use a comparable type as the map key. For example, pick a unique string for every function you will use, maybe pair the two in a struct, use the string as the map key. </p> <p>Though this worked for my use case, it was still less than satisfying. I still haven&#39;t found a compelling way to create a &#34;set&#34; of functions in Go. I welcome suggestions. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

781 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传