1 回答
TA贡献1831条经验 获得超9个赞
使用地图完成上述操作 https://play.golang.org/p/8C5M0L-es6o
package main
import (
"fmt"
"strings"
)
// Table with Fields and Assoc
type Table struct {
Name string
Assoc map[string]int
}
// Assoc is a name of associated Table
// type Assoc struct {
// Name string
// }
func main() {
tables := []string{
"a",
"b",
"c",
"d",
"f",
"a_b",
"a_c",
"a_d_f",
"c_d",
}
var tbls = make(map[string]map[string]int)
for _, t := range tables {
if strings.Contains(t, "_") {
splitAssocs := strings.Split(t, "_")
for i:=0;i<=len(splitAssocs)-2;i++ {
for j:=(i+1);j<=len(splitAssocs)-1;j++{
_, ok := tbls[splitAssocs[i]]
if !ok{
tbls[splitAssocs[i]] = make(map[string]int)
}
_, ok = tbls[splitAssocs[j]]
if !ok{
tbls[splitAssocs[j]] = make(map[string]int)
}
tbls[splitAssocs[i]][splitAssocs[j]] = 1
tbls[splitAssocs[j]][splitAssocs[i]] = 1
}
}
} else {
_, ok := tbls[t]
if !ok{
tbls[t] = make(map[string]int)
}
}
}
fmt.Println(tbls)
}
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报