passing a string slice to a variadic function expecting []interface{} or []any

64 views
Skip to first unread message

Peter Galbavy

unread,
Jun 22, 2022, 10:58:11 AM6/22/22
to golang-nuts
This is probably an FAQ but...

I've tried to understand this before but gave up; I am trying to (quick and dirty) import from a CSV or Excel file to a DB and I would like to take the slice from csv.Read() and just pass it through to stmt.Exec() but because the first is a []string and the second a []interface{} or []any (in 1.18+) it baulks. e.g.

  fields, err := csv.Read()
  ... checks
  _, err = stmt.Exec(fields)

I got round it by looping over the slice and putting appending the strings to a []interface{} local.

Is there a simpler way to do this inline - please ignore the issues around the contents or the number of elements for now.

Axel Wagner

unread,
Jun 22, 2022, 11:05:17 AM6/22/22
to Peter Galbavy, golang-nuts
No, there is no way to do it. You have to make a copy of the slice as you did.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/3c20e19d-8805-4f76-b52b-a3b8275ea708n%40googlegroups.com.

Peter Galbavy

unread,
Jun 22, 2022, 11:10:43 AM6/22/22
to golang-nuts
Thanks for setting my mind at ease.
Reply all
Reply to author
Forward
0 new messages