0

How do I convert the following slice of struct to slice of interface?

type BatchImportData struct {
    Name string
    SetHash []string
    SetMembers []string
}

var b []BatchImportData

This is so I can batch import data into Neo4j, as the Neo4j driver requires a slice of interface when the slice of struct is passed as a parameter.

https://github.com/neo4j/neo4j-go-driver

Cypher Type Driver Type

List []interface{}

mbudge
  • 557
  • 13
  • 28

2 Answers2

2

You can loop over the struct slice and append it to the interface slice.

    var b []BatchImportData = []BatchImportData{}

    var c []interface{}

    for _, b := range b {
        c = append(c, b)
    }
edkeveked
  • 17,989
  • 10
  • 55
  • 93
1

I think your doubt may be something along the lines of this, in which case you may find this answer quite helpful.

So you will need to loop over the slice and copy each element. Something like:

var b []BatchImportData
interfaceSlice := make([]interface{}, len(b))
for i, v := range b {
  interfaceSlice[i] = v
}
Vaibhav Mishra
  • 415
  • 3
  • 10