Returning an array of arrays?

agolangf · 2017-05-21 05:00:10 · 640 次点击    
这是一个分享于 2017-05-21 05:00:10 的资源,其中的信息可能已经有所发展或是发生改变。

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'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?

so instead of doing something like

type arr struct{ nums []int }

func figureItOut() []arr{

}

I could do something like

func doSomething() []{[]int}{

}

I tried that but it doesn't seem to like it. Googled it and couldn't find anything clear. I just want to return an array of int arrays. I


评论:

Frakturfreund:

The correct syntax to do this is

func doSomething() [][]int {

}

For further informations, read Effective Go: Two-dimensional slices.

Also note that this a slice of slices, not an array of arrays.

dchapes:

I tried [random syntax] but [the Go compiler] doesn't seem to like it. Googled it […]

Gah! Instead of trying random syntax and hoping if it compiles and maybe does what you want, or doing web searches, or asking online, just read the (relatively short) spec and learn the language.

RealityMan_:

Is it really necessary to be a dick? You'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

I'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'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't know, because you've never asked a question about a language in your life. I envy you.

I do a lot of help in home networking, you know how many times I could answer with, "the answer is in the user manual?" But instead, I actually take the time to help them instead of berate them. What's the point? They are probably already embarrassed they even need to ask the question. If you're annoyed by the question, feel free to not answer. If you make an answer easily accessible maybe the next "googler" will find it and not ask it the next time.

:

[deleted]

BloodOfSteel:

what is the difference between type and struct ?

Killing_Spark:

A type is an superset for structs. A type can mean a struct, or an interface or some primitive type like int or float


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

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