代码拉取完成,页面将自动刷新
package opc
import "fmt"
//Tree creates an OPC browser representation
type Tree struct {
Name string
Parent *Tree
Branches []*Tree
Leaves []Leaf
}
//Leaf contains the OPC tag and forms part of the Tree struct for the OPC browser
type Leaf struct {
Name string
Tag string
}
//ExtractBranchByName return substree with name
func ExtractBranchByName(tree *Tree, name string) *Tree {
if tree.Name == name {
return tree
}
for _, b := range tree.Branches {
subtree := ExtractBranchByName(b, name)
if subtree != nil {
return subtree
}
}
return nil
}
//CollectTags traverses tree and collects all tags in string slice
func CollectTags(tree *Tree) []string {
collection := []string{}
for _, l := range tree.Leaves {
collection = append(collection, l.Tag)
}
for _, b := range tree.Branches {
lowerCollection := CollectTags(b)
collection = append(collection, lowerCollection...)
}
return collection
}
//PrettyPrint prints tree in a nice format
func PrettyPrint(tree *Tree) {
fmt.Println(tree.Name)
printSubtree(tree, 1)
}
// printSubtree is a recursive helper function to traverse the tree
func printSubtree(tree *Tree, level int) {
space := ""
for i := 0; i < level; i++ {
space += " "
}
for _, l := range tree.Leaves {
fmt.Println(space, "-", l.Tag)
}
for _, b := range tree.Branches {
fmt.Println(space, "+", b.Name)
printSubtree(b, level+1)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。