Is it possible in Go create array with where each element of array will be array of slices or structures.
Something like in PHP
$a = [1=>"test", 2=>""]
// in this example 2 is integer will be for GoLang?
$a[2] = [ object, object, object ]
Can I do in Go something like ? I know about incorrect syntax.
var a [int][]StructureName
b := make([]StructureName, 0)
b := append ( b, StructureName{a, b, c, d})
b := append ( b, StructureName{e, f, g, h})
a[0] = append (a[0][0], b)
`/*
[
1 => [
‘username1’, <-- string element
‘events’=>[ <-- array of structures
object1, <-- structure
object2, <-- structure
object3 <-- structure
]
],
2 => [ <-- next record
‘username2’,
‘events’=>[
object1,
object2,
object3
]
]
]
*/
`
You can use map and map it from int => interface{}.
package main
import (
"fmt"
)
func main() {
m := make(map[int]interface{})
var x []interface{}
x = append(x, "username")
x = append(x, []int{3, 1, 3})
m[1] = x
fmt.Println(m)
}
https://play.golang.org/p/B_dsvARic8c
Hope this helps.
Declare types that match the structure of the data. This ensures type safety and avoids the need to type assert when accessing the data.
In this case, it looks like you need a slice of structs where on of the fields is also a slice of event structs.
type Event struct {
// Even fields TBD
}
type User struct {
Name string
Events []*Event
}
var users []*User
users = append(users, &User{
Name: "name1",
Events: []*Event{ object1, object2, object3 },
})