On Thu, Mar 19, 2020 at 5:21 PM <yvonn...@outlook.com> wrote: > > I have an array and the length of this array is dynamic. I want to pass this > array's values as a list of parameters to a function. Can you please suggest > what is a good way to do this? > > > For example, func(query string, args ...interface{}). My array is > arrayExample := []string {"1", "a", "b", "2"}. Then it will be something like > func(query, arrayExample[0], arrayExample[1], arrayExample[2], > arrayExample[3]). > > If my array is arrayExample := []string {"1", "a"}. Then it will be > something like func(query, arrayExample[0], arrayExample[1]) > > But as I mentioned the array is dynamic so the length can be any number. I > can get the length, but how can I pass each member in the array to the > function call as a list of parameters?
Note that what you are calling an array, Go calls a slice. Write this as `f(query, arrayExample...)`. See https://golang.org/ref/spec#Passing_arguments_to_..._parameters . Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXdmJNjkXLS1pW%2BzOnGyH%3Dg_LUo5ut1keN%3DiMDsxLw4MA%40mail.gmail.com.