Variadic Parameters...https://play.golang.org/p/l6pbuFDz_eP

https://play.golang.org/p/l6pbuFDz_eP

I’m not sure how to ask these questions, but I’ll give it a shot.

The above code yields
[2 3 4 5 6 7 8 9]
[]int
for item in index position 0 we are now adding 2 to the total which is now 2
for item in index position 1 we are now adding 3 to the total which is now 5
for item in index position 2 we are now adding 4 to the total which is now 9
for item in index position 3 we are now adding 5 to the total which is now 14
for item in index position 4 we are now adding 6 to the total which is now 20
for item in index position 5 we are now adding 7 to the total which is now 27
for item in index position 6 we are now adding 8 to the total which is now 35
for item in index position 7 we are now adding 9 to the total which is now 44
The total is 44
The total is 44

Which part of this is executed (correct word?) by
fmt.Println(“The total is”, x)

That’s my first question.

Also, which part of this is executed (correct word?) by

fmt.Println(“The total is”, sum)
return sum

More: Which part of the above results from
fmt.Println(“The total is”, sum)
return sum

More: Which part of the above results from
fmt.Println(“The total is”, sum)
return sum

Never mind about this part. I got it.

It’s starting to make sense to me because previously, I did not notice this part

sum += v

1 Like

Here’s more code:
https://play.golang.org/p/doo199ZzYtT

I don’t understand what

xi := []int{2, 3, 4, 5, 6, 7, 8, 9}

is doing. It doesn’t seem to have different results than

x := sum(2, 3, 4, 5, 6, 7, 8, 9)

in the previous code.

Why does x := sum(2, 3, 4, 5, 6, 7, 8, 9) make a slice of string?

Hi Cherolyn,

xi := []int{2, 3, 4, 5, 6, 7, 8, 9}

This declares and initializes a slice of ints, then assigns it to the variable xi.
It is the same as

var xi []int
xi = []int{2, 3, 4, 5, 6, 7, 8, 9}

The first line can be read as, “Variable xi is a slice of ints”. (Read [] as “slice”.) The second line is simply how slices are initialized.

In your code on line 9, you are passing the slice to a function as

x := sum(xi...)

Becausesum() is a variadic function, the slice is sent to sum() in the same way as in

sum(2, 3, 4, 5, 6, 7, 8, 9)

In each case, the underlying mechanism is that sum() receives a slice. It just looks different in the code.

I don’t know if it will help, but this is in the spec here:

https://golang.org/ref/spec#Passing_arguments_to_…_parameters

(I hope I got all that right - I am a little too busy today and didn’t have time to check it.)

1 Like

This does help and you got it right. I think I’m going to have to study this more than once. This is a very interesting statement.

Otherwise, the value passed is a new slice of type []T with a new underlying array whose successive elements are the actual arguments, which all must be assignable to T .

Here is some more code on the same subject:
https://play.golang.org/p/OwhQgVOTg7J

I have several questions.

1)What part of the code results in: “[]int”
2)Is this part of the code necessary? And, if so, why? “return sum”

  1. fmt.Printf("%T\n", x) Here you print the type of variable x
  2. Return sum means you want the caller receives the result of the function call.
1 Like

func sum(x …int) int {

Why is it necessary to write “int” twice?

Because the first one is the type of the parameter(s) and the second one is the type of the return value.

1 Like

Who or what is " the caller"

In this context it refers to the place in code where the function has been called.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.