Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ... mean when coming directly after a slice?

Tags:

go

What does ... mean in this context in Go?

ids = append(ids[:index], ids[index+1:]...)

I have read this great Q&A: Do three dots (which is called wildcard?) contain multiple meanings?

about what ... means in some situations, but I don't understand what it means in the situation above.

like image 560
user1283776 Avatar asked Jun 01 '16 13:06

user1283776


2 Answers

Some languages (C, Python, ...) accept variadic arguments. Basically, you allow the client of a function to pass a number of arguments, without specifying how many. Since the function will still need to process these arguments one way or another, they are usually converted to a collection of some sort. For instance, in Python:

def foo(x, *args): # * is used for variadic arguments
    return len(args)

>>> foo(1)  # Passed the required argument, but no varargs
0 
>>> foo(1, 2, 3)  # Passed the required, plus two variadic arguments
2
>>> foo(1, 2, 3, 4, 5, 6) # required + 5 args, etc...
5

Now one obvious problem of that approach is that a number of arguments is quite a fuzzy concept as far as types are concerned. C uses pointers, Python doesn't really care about types that much in the first place, and Go takes the decision of restricting it to a specified case: you pass a slice of a given type.

This is nice because it lets the type-system do its thing, while still being quite flexible (in particular, the type in question can be an interface, so you can pass different 'actual types' as long as the function knows how to process these.

The typical example would be the Command function, which executes a program, passing it some arguments:

func Command(name string, arg ...string) *Cmd

It makes a lot of sense here, but recall that variadic arguments are just a convenient way to pass slices. You could have the exact same API with:

func Command(name string, args []string) *Cmd

The only advantage of the first is that it lets you pass no argument, one argument, several... without having to build the slice yourself.

What about your question then ?

Sometimes, you do have a slice, but need to call a variadic function. But if you do it naively:

my_args_slice := []string{"foo", "bar"}
cmd := Command("myprogram", my_args_slice)

The compiler will complain that it expects string(s), but got a slice instead ! What you want to tell it is that it doesn't have to 'build the slice in the back', because you have a slice already. You do that by using this ellipsis:

my_args_slice := []string{"foo", "bar"}
cmd := Command("myprogram", my_args_slice...) // <- Interpret that as strings

The append function, despite being built-in and special, follows the same rules. You can append zero, one, or more elements to the slice. If you want to concatenate slices (i.e. you have a 'slice of arguments' already), you similarly use the ellipsis to make it use it directly.

like image 138
val Avatar answered Oct 09 '22 17:10

val


It unpacks slice.

ids[:index] is a short form of ids[0:index]

ids[index+1:] is a short form of ids[index+1:len(ids)-1]

So, your example ids = append(ids[:index], ids[index+1:]...) translates to

//pseudocode
ids = append(ids[0:index], ids[index+1], ids[index+2], ids[index+3], ..., ids[len(ids)-2], ids[len(ids)-1])
like image 23
Darigaaz Avatar answered Oct 09 '22 19:10

Darigaaz