M
M
Mr Freeman2017-01-12 11:50:54
go
Mr Freeman, 2017-01-12 11:50:54

Is it possible to make a function that returns an indefinite number of values?

Hi all. In general, there is a function from the vendor that takes an indefinite number of arguments, such as

func NewFoo(bar ...int) *FooType {
  return Foo
}

Here, I also need to insert an indefinite number of values ​​into it, for example,
I have a sliceBar := [1, 4, 5] - it can be as many as you like, and different. I would like to insert something like NewFoo(sliceBar) - but accordingly, so that there is a number of values, not a slice. Even with a cycle, I don’t know how this can be implemented in GO.
There was an idea to make an anonymous function for inserting into NewFoo, but it also fails to implement multiple undefined return of arguments.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-01-12
@vladamir

Sorry, at first I misunderstood the question, I thought that you need to return an indefinite number of values.
To send a slice as a set of values ​​to a function that accepts an indefinite number of values, you need to do this:
https://play.golang.org/p/r98qBIvCC8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question