Working with Complex Data Structs

I've looked at a bunch of examples in Go showing how to read and write data to a neo4j db, however, those examples are working with pretty basic data/queries.

Is there a more "Go" way of inserting data that comes in a struct like this:

person := Person {
      name: "Bob"
       age: 59
       kids :[]Kids {
       {id: 0, name: "kid1"},
      {id: 1, name: "kid2"},
    }
}

I've been able to do it by basically building/appending to a string. Is there a more idiomatic way of handling more complex data or is the only option for string parsing?

func buildQuery(p Person) (string, map[string]any) {
	params := map[string]any{
		"name":  p.Name,
                 "age": p.Age
	}

	baseQuery := `CREATE(p:Person {name: $name, age: $age}),`
	for i, kid := range p.Kids {
		kidName := kid.Name
		varName := "kid_name" + fmt.Sprintf("%d", i)
		baseQuery += fmt.Sprintf(`(%s:Kid {name: $%s}),`, kidName, varName)
		baseQuery += fmt.Sprintf(`(%s)-[:CHILD_OF]->(p),`, kidName)
		params[varName] = kidName
	}
	returnQuery := baseQuery[:len(baseQuery)-1] + "RETURN p"
	return returnQuery, params
}

What you can do is transform the kid slice into a parameter typed as a list of maps.
Then, you can rely on Cypher's UNWIND to iterate over each kid.

func buildQuery(p Person) (string, map[string]any) {
	kidParameters := make([]map[string]any, len(p.Kids))
	for i, kid := range p.Kids {
		kidParameters[i] = map[string]any{
			"name": kid.Name,
		}
	}
	params := map[string]any{
		"name": p.Name,
		"age":  p.Age,
		"kids": kidParameters,
	}
	return `
CREATE (p:Person {name: $name, age: $age}) 
UNWIND $kids AS kid
CREATE (:Kid {name: kid.name})-[:CHILD_OF]->(p)
RETURN p
`, params
}

As a result, everything is passed as a parameter and you don't need any more string interpolation.

There are some discussions in the drivers team to ease the conversion of structs to parameters, but they are just experiments at this point and may or may not ship at some point in the future.

That is WAY cleaner then what I was doing, lol! Thanks a lot for the suggestion, I appreciate it. It looks like I have to add in a WITH statement and pass in p between the first CREATE and UNWIND.

Still new to Neo4j and Cypher but really enjoying it so far!

Sorry, I had not tested the query in full.
Glad you figured it out!
Enjoy Neo4j and keep your questions coming! :)