Returning an array of arrays?

agolangf · · 444 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So I know I can create a struct and have it contain the array and then return an array of the objects, but is there a way I can just do it in the method definition. I&#39;ll probably create one anyway, because I need a place to store my arrays before I return them, but was wondering if there was an easier way?</p> <p>so instead of doing something like</p> <p>type arr struct{ nums []int }</p> <p>func figureItOut() []arr{</p> <p>}</p> <p>I could do something like</p> <p>func doSomething() []{[]int}{</p> <p>}</p> <p>I tried that but it doesn&#39;t seem to like it. Googled it and couldn&#39;t find anything clear. I just want to return an array of int arrays. I</p> <hr/>**评论:**<br/><br/>Frakturfreund: <pre><p>The correct syntax to do this is</p> <p>func doSomething() [][]int {</p> <p>}</p> <p>For further informations, read <a href="https://golang.org/doc/effective_go.html#two_dimensional_slices" rel="nofollow">Effective Go: Two-dimensional slices</a>.</p> <p>Also note that this a slice of <a href="https://golang.org/doc/effective_go.html#slices" rel="nofollow">slices</a>, not an array of <a href="https://golang.org/doc/effective_go.html#arrays" rel="nofollow">arrays</a>.</p></pre>dchapes: <pre><blockquote> <p>I tried [random syntax] but [the Go compiler] doesn&#39;t seem to like it. Googled it […]</p> </blockquote> <p>Gah! Instead of trying random syntax and hoping if it compiles and maybe does what you want, or doing web searches, or asking online, <a href="https://golang.org/ref/spec" rel="nofollow">just read the (relatively short) spec and <em>learn the language</em></a>.</p></pre>RealityMan_: <pre><p>Is it really necessary to be a dick? You&#39;re right though, no one could EVER have questions about a language after reading the language spec. I guess that is why stack overflow is so devoid of programming related questions. /s </p> <p>I&#39;m sure this has never happened to you, since you are some uber developer, but sometimes people get mental blocks and just need to talk through the problem. It&#39;s especially easy to do when you are learning a new language, and are often forced to absorb a lot of new information at once. Again, you wouldn&#39;t know, because you&#39;ve never asked a question about a language in your life. I envy you.</p> <p>I do a lot of help in home networking, you know how many times I could answer with, &#34;the answer is in the user manual?&#34; But instead, I actually take the time to help them instead of berate them. What&#39;s the point? They are probably already embarrassed they even need to ask the question. If you&#39;re annoyed by the question, feel free to not answer. If you make an answer easily accessible maybe the next &#34;googler&#34; will find it and not ask it the next time.</p></pre>: <pre><p>[deleted]</p></pre>BloodOfSteel: <pre><p>what is the difference between type and struct ?</p></pre>Killing_Spark: <pre><p>A type is an superset for structs. A type can mean a struct, or an interface or some primitive type like int or float</p></pre>

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

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