On Fri, May 3, 2019 at 7:57 AM Louki Sumirniy <louki.sumirniy.stal...@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 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.