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:
dchapes: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.
RealityMan_: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.
: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.
BloodOfSteel:[deleted]
Killing_Spark:what is the difference between type and struct ?
A type is an superset for structs. A type can mean a struct, or an interface or some primitive type like int or float
