go newbie issue

agolangf · · 427 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>hi everyone, i just started go today and was working through some problems from codeforces and am currently on this problem <a href="http://codeforces.com/problemset/problem/96/A" rel="nofollow">http://codeforces.com/problemset/problem/96/A</a> and trying to do it with go but it says I cannot have a non constant length of array but I have no idea to work around this. If I could get some help thanks. Here is my code: &lt;script <a href="https://gist.github.com/gauravdesale/bb48bf0af6a9a1bb0813632bb56f66bd" rel="nofollow">https://gist.github.com/gauravdesale/bb48bf0af6a9a1bb0813632bb56f66bd</a></p> <hr/>**评论:**<br/><br/>wybiral: <pre><p>You can define it as a slice and then &#34;make&#34; the array (allocate at runtime). <a href="https://play.golang.org/p/G7xuk8hiJu" rel="nofollow">https://play.golang.org/p/G7xuk8hiJu</a></p> <p>But it will still fail because your i+j causes an array out-of-bounds access.</p> <p>EDIT: I also changed some of your assignments. You only use &#34;:=&#34; to declare AND assign. So if you already used &#34;var&#34; to declare then don&#39;t use &#34;:=&#34; to assign (just use &#34;=&#34;). Or just don&#39;t use &#34;var&#34; and declare with &#34;:=&#34;.</p></pre>0xjnml: <pre><ul> <li><a href="https://golang.org/ref/spec#Array_types" rel="nofollow">Array Types</a></li> <li><a href="https://golang.org/ref/spec#Slice_types" rel="nofollow">Slice Types</a></li> </ul></pre>

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

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