<p>Go doesn't have generics except for certain built in types (i.e., maps, slices, channels, and arrays). Is there any information on how these generic types are implemented underneath the covers? I'm not readily finding this info and am very curious about the approach taken.</p>
<p>Edit: I am <em>not</em> talking about how to implement custom generics in Go. I know that those don't exist (though some, including me, have hopes for Go 2.0). I'm asking how the exiting Go language implements generics <em>just</em> for the built in maps, channels, slices and arrays. The compiler is doing something when I call make, and I'd like to know what is going on.</p>
<hr/>**评论:**<br/><br/>comrade_donkey: <pre><p>Look at e.g. <a href="https://github.com/golang/go/blob/365594ad59873cd8f7fde5ec158067bf695185ee/src/runtime/slice.go#L39"><code>runtime.makeslice</code></a>. It's what the compiler replaces <code>make([]T, n, m)</code> with. There's also <a href="https://github.com/golang/go/blob/cbc4e5d9c4f2444c5d40ae6333b4e1f4c9cfbd41/src/runtime/hashmap.go#L270"><code>runtime.makemap</code></a> and analogous functions for other <del>types</del> kinds.</p></pre>unm95: <pre><p><a href="https://www.youtube.com/watch?v=Tl7mi9QmLns" rel="nofollow">https://www.youtube.com/watch?v=Tl7mi9QmLns</a></p></pre>_youtubot_: <pre><p>Video linked by <a href="/u/unm95" rel="nofollow">/u/unm95</a>:</p>
<table><thead>
<tr>
<th align="center">Title</th>
<th align="center">Channel</th>
<th align="center">Published</th>
<th align="center">Duration</th>
<th align="center">Likes</th>
<th align="center">Total Views</th>
</tr>
</thead><tbody>
<tr>
<td align="center"><a href="https://youtube.com/watch?v=Tl7mi9QmLns" rel="nofollow">GopherCon 2016: Keith Randall - Inside the Map Implementation</a></td>
<td align="center">Gopher Academy</td>
<td align="center">2016-08-18</td>
<td align="center">0:26:50</td>
<td align="center">92+ (100%)</td>
<td align="center">7,206</td>
</tr>
</tbody></table>
<hr/>
<p><a href="https://np.reddit.com/r/youtubot/wiki/index" rel="nofollow"><sup>Info</sup></a> <sup>|</sup> <a href="https://np.reddit.com/message/compose/?to=_youtubot_&subject=delete%20comment&message=dm0iz49%0A%0AReason%3A%20%2A%2Aplease+help+us+improve%2A%2A" rel="nofollow"><sup>/u/unm95</sup> <sup>can</sup> <sup>delete</sup></a> <sup>|</sup> <sup>v1.1.3b</sup></p></pre>gac_web: <pre><p><a href="https://blog.golang.org/slices" rel="nofollow">https://blog.golang.org/slices</a></p>
<p>And Go source is written in Go which makes it relatively easy to read if you know Go.</p></pre>drvd: <pre><p>This is not done as "generics", these are just types. E.g. <code>struct{a int}</code> and <code>struct{b string}</code> are not treated as specialisations of a generic struct, but well, just types. Same for <code>[]int</code> vs. <code>[]string</code>.</p>
<p>The only halfway "generic" is in make, copy and append which just implement what the language spec says about it. These are pretty simple (no "generics" used here). E.g. make just allocates cap*sizeof(elementType) bytes and "zeros" them</p>
<p>You won't find anything useful there to implement generics.</p></pre>itsmontoya: <pre><p>Have you read the source code? It's literally using generics under the hood</p></pre>drvd: <pre><p>Yes I did. Not it is not. Making a slice is not much more than</p>
<pre><code>p := mallocgc(et.size*uintptr(cap), et, true)
return slice{p, len, cap}
</code></pre>
<p>No generics.</p></pre>bartoszgolek: <pre><p>Actualy, there is no way to implement custom generics in Go. There still is discusion undergoing about it, and hopefully generics will be implemented in Go 2.0.</p></pre>jlund3: <pre><p>Edited my post for clarification :)</p></pre>10F1: <pre><p>TL;DR compiler magic.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传