0

how to invoke a method with below signature

SomeFunc( args ...interface{})

with variable of type []interface{} Is it possible to invoke the above method? If yes how?

Thanks

Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37
  • 1
    There's not a lot of information to go on here, but if I understand correctly you should be able to just call the function and pass it your two arguments. https://play.golang.org/p/ZUPbJYYCt7 –  Dec 29 '16 at 08:38
  • 1
    Actually my comment above may not work in your specific case. The issue is that you've named the function starting with a lowercase letter which in Go means that it's a private function and is only available within its own package. You should start any publicly accessible functions (any functions that will be invoked outside of the current package) with a capital letter - for example: `SomeFunc` –  Dec 29 '16 at 08:41
  • @Mike thanks , but your code is taking the interface array as a single interface in the SomeFunc method https://play.golang.org/p/9dK4QjmW0U – Jishnu Prathap Dec 29 '16 at 09:19
  • Variadic arguments are collapsed into a single array of values regardless of how many arguments are passed. Here's another example which passes two separate values. https://play.golang.org/p/vvsfgSAk57 I added the `#` formatter to show the Golang representation of the object. You can index into the argument array as you would any other array, for example `args[0]` extracts the first value. –  Dec 29 '16 at 09:20
  • Yes that is the issue I am facing – Jishnu Prathap Dec 29 '16 at 09:27
  • @Mike invoking like this solved the problem SomeFunc(a, b...) – Jishnu Prathap Dec 29 '16 at 09:29
  • 1
    Glad you found something that worked for you. :) –  Dec 29 '16 at 09:31
  • 1
    Possible duplicate of [Unpacking slice of slices](http://stackoverflow.com/questions/39914447/unpacking-slice-of-slices/39915311#39915311); and [Golang Join array interface](http://stackoverflow.com/questions/36125024/golang-join-array-interface/36125119#36125119). – icza Dec 29 '16 at 13:28

1 Answers1

2
func main() {
    b := []interface{}{"hello", "Hi"}
    SomeFunc(b...) 
}

Solved the issue by using ... after b array.

For more details plz see Unpacking slice of slices and Golang Join array interface

Community
  • 1
  • 1
Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37