-1

I'm trying to understand the Wire library in Golang and find that in the wire.go there's a function looks like this:

func NewSet(...interface{}) ProviderSet {
    return ProviderSet{}
}

It looks foreign to me as to why the ...interface{}) parameter is unnamed (meaning not being used inside the function) but then the caller still passes meaningful values to it?

var Set = wire.NewSet(
    wire.Value(Foo(41)),
    provideFooBar)
Khanh Tran
  • 1,776
  • 5
  • 25
  • 48

1 Answers1

3

Parameters being named or unnamed has nothing to do with whether the caller having to pass values for them. Being unnamed just means they cannot be used (cannot be referred to) inside the function.

NewSet has a variadic parameter, which means any number of arguments may be passed to it that can be assigned to the type, and any value can be assigned to interface{} (all value implements the empty interface).

The "empty" implementation of NewSet() you see is just a placeholder for documentation and compilers. The generated code will use the passed arguments.

If you have a function:

func dummy(int) {}

You can't call it like dummy(), that's a compile-time error. You can only call it by passing an int value to it, e.g. dummy(1).

See related: Is unnamed arguments a thing in Go?

icza
  • 389,944
  • 63
  • 907
  • 827
  • As I can see that the caller does provide meaningful values for those params, not just dump values to satisfied the signature. Why is that? – Khanh Tran May 13 '20 at 09:28
  • 2
    @petwho The "empty" implementation of `NewSet()` you see is just a placeholder for documentation and compilers. The generated code will use the passed arguments. – icza May 13 '20 at 09:29
  • I am gonna accept your answer, can you add this comment to your answer so that other people reading it can have this info too without having to read comment? – Khanh Tran May 13 '20 at 09:35