Consider this: var something []interface{} var anotherThing []SomeType
append(something, anotherThing) Does the compiler know here whether I want to append "anotherThing" or " anotherThing..."? It has no way to know, and both would work, so that's why we give it a hint with the ellipsis symbol. Sure, you could sometimes infer intent from the types, but then you would have an append() function with many behavior special cases. -- Marcin On Fri, May 3, 2019 at 10:24 AM Louki Sumirniy < louki.sumirniy.stal...@gmail.com> wrote: > Whenever I write appends and I'm splicing slices together, I often get an > error saying the second slice is the wrong type (it wants the slice element > type). So, doesn't that mean the trailing ellipsis is like an iterator > feeding out one element at a time? Is there some reason this is needed, > because if slice types match this unrolling is implicit, I mean, the > programmer obviously intends the two slices be spliced into one new one... > > On Friday, 3 May 2019 19:13:02 UTC+2, Ian Lance Taylor wrote: >> >> On Fri, May 3, 2019 at 7:57 AM Louki Sumirniy >> <louki.sumi...@gmail.com> wrote: >> > >> > Ellipsis makes the parameter type into a slice, but in append it makes >> the append repeat for each element, or do I misunderstand this? >> > >> > There is a syntactic distinction between them too. Parameters it is a >> prefix to the type, append it is a suffix to the name. It neatly alludes to >> the direction in which the affected variable is operated on - inside the >> function name ...type means name []type and for append, we are splitting >> the slice into a tuple (internally), at least as I understand it, and the >> parameter is the opposite, tuple to slice. >> > >> > I sometimes lament the lack of a tuple type in Go (I previously worked >> a lot with Python and PHP), but []interface{} isn't that much more >> difficult and the ellipsis syntax is quite handy for these cases - usually >> loading or otherwise modifying essentially a super simple container array. >> >> For any function F and some type T declared as >> >> func F(x ...T) {} >> >> within F x will have type []T. You can call F with a slice s of type []T >> as >> >> F(s...) >> >> That will pass the slice s to F as the final parameter. This works >> for any variadic function F. >> >> The append function is implicitly declared as >> >> func append(to []byte, add ...byte) >> >> You can call it as >> >> append(to, add...) >> >> Here F is append and T is byte. >> >> There is a special case for append with an argument of type string, >> but other than that append is just like any other variadic function. >> >> Ian >> >> >> >> > On Friday, 3 May 2019 16:44:47 UTC+2, Ian Lance Taylor wrote: >> >> >> >> On Fri, May 3, 2019 at 7:34 AM Louki Sumirniy >> >> <louki.sumi...@gmail.com> wrote: >> >> > >> >> > The ellipsis has two uses in Go, one is in variadic parameters, the >> other is in the slice append operator. It is essentially an iterator that >> takes a list and turns it into a slice (parameters) or takes a slice and >> turns it into a recursive iteration (append). Parameters with the ellipsis >> are addressed inside the function as a slice of the type after the >> ellipsis. >> >> >> >> Note that there is nothing special about append here, it's just like >> >> passing a slice to any other variadic parameter. See >> >> https://golang.org/ref/spec#Passing_arguments_to_..._parameters . >> >> >> >> Ian >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups "golang-nuts" group. >> > To unsubscribe from this group and stop receiving emails from it, send >> an email to golan...@googlegroups.com. >> > For more options, visit https://groups.google.com/d/optout. >> > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.