Declaring structs and slices and arrays

xuanbao · · 856 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I know I&#39;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 {&#34;first&#34;,&#34;second&#34;}, Production: &#34;Me&#34;, Year: &#34;2012&#34;, }, } </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&#39;t understand is why I have to twice declare that Title is a []string; first in Credits and secondly in MovieCredits. Under MovieCredits, I&#39;m saying Title is a slice of strings but I&#39;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&#39;t I have to put &#34;string&#34; 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. &#34;Me&#34; and &#34;2012&#34; are string literals therefore expressions.</p></pre>dhdfdh: <pre><p>Good answer. Thanks.</p></pre>

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

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