<p>Question says it all. When you have a variable and you see this express: <code>myvar.(string)</code>, what does that mean?</p>
<hr/>**评论:**<br/><br/>gbrlsnchs: <pre><p>From <a href="https://tour.golang.org/methods/15:" rel="nofollow">https://tour.golang.org/methods/15:</a></p>
<p>Type assertions
A type assertion provides access to an interface value's underlying concrete value.</p>
<p><code>t := i.(T)</code></p>
<p>This statement asserts that the interface value i holds the concrete type <code>T</code> and assigns the underlying <code>T</code> value to the variable <code>t</code>.</p>
<p>If <code>i</code> does not hold a T, the statement will trigger a panic.</p>
<p>To <em>test</em> whether an interface value holds a specific type, a type assertion can return two values: the underlying value and a boolean value that reports whether the assertion succeeded.</p>
<p><code>t, ok := i.(T)</code></p>
<p>If <code>i</code> holds a <code>T</code>, then <code>t</code> will be the underlying value and <code>ok</code> will be true.</p>
<p>If not, <code>ok</code> will be false and t will be the zero value of type <code>T</code>, and no panic occurs.</p>
<p>Note the similarity between this syntax and that of reading from a map.</p></pre>linuxfan612: <pre><p>Perfect thanks!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传