<p>I know I'm forgetting something about slices vs arrays but I did this:</p>
<pre><code>type Credits struct {
Title []string
Production string
Year string
}
MovieCredits: []Credits {
{
Title: []string {"first","second"},
Production: "Me",
Year: "2012",
},
}
</code></pre>
<p>So when I do </p>
<pre><code>log.Print(MovieCredits[0].Title[0]
</code></pre>
<p>It prints out the title. </p>
<p>What I don't understand is why I have to twice declare that Title is a []string; first in Credits and secondly in MovieCredits. Under MovieCredits, I'm saying Title is a slice of strings but I'm saying it again under MovieCredits. Or am I just lucky that it works?</p>
<p>If the []string is required for Title, why don't I have to put "string" for Production and Year under MovieCredits?</p>
<hr/>**评论:**<br/><br/>kl9va: <pre><p>statements vs expressions</p>
<p>[]string is a statement , []string{} is an expression that yields a result. "Me" and "2012" are string literals therefore expressions.</p></pre>dhdfdh: <pre><p>Good answer. Thanks.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传