I would like to loop on the name of my tables to add the associations defined by the symbol "_
".
If table a_b
and a
then b
exists then a = [b]
, b = [a]
.
Finally, I do not have to print the tables containing a "_
" in the name
Struct
// Table with Fields and Assoc
type Table struct {
Name string
Assoc []Assoc
}
// Assoc is a name of associated Table
type Assoc struct {
Name string
}
tables := []string{
"a",
"b",
"c",
"d",
"f",
"a_b",
"a_c",
"a_d_f",
"c_d",
}
var tbls []Table
for _, t := range tables {
if strings.Contains(t, "_") {
// Split to find "_" like assoc := strings.Split(t, "_") ?
// append in struct "Table{Name:a, Assoc: [b,c,d,f]}"
// append in struct "Table{Name:b, Assoc: [a]}"
// append in struct "Table{Name:c, Assoc: [a,d]}"
// append in struct "Table{Name:d, Assoc: [a,c,f]}"
// append in struct "Table{Name:f, Assoc: [a,d]}"
} else {
n := Table{
Name: t,
}
tbls = append(tbls, n)
}
}
Return like fmt.Println(tbls)
:
[{a [b,c,d,f]} {b [a]} {c [a,d]} {d [a,c,f]} {f [a,d]}]