首页 > IT职业 > IT职场 > 【Go语言干货】从入门说起,你不得不要知道的面向对象


type TreeNode struct{
Left,Right *TreeNode
Value int
}
func (node Node) Print() {
fmt.Print(node.Value, " ")
}
func (node *Node) SetValue(value int) {
node.Value = value
}
func CreateNode(value int) *Node {
return &Node{Value: value}
}
func main() {
var root treeNode
fmt.Println(root) nil nil 0
}
func(node TreeNode) print(){
fmt.Print(node.Value)
}
func(node *TreeNode) setValue(value int) {
if node==nil{
fmt.Println("Setting value to nil"+ "node.Ignored.")r
return
}
node.Value=value
}
func (node *Node) Traverse() {
node.TraverseFunc(func(n *Node) {
n.Print()
})
fmt.Println()
}
func (node *Node) TraverseFunc(f func(*Node)) {
if node == nil {
return
}
node.Left.TraverseFunc(f)
f(node)
node.Right.TraverseFunc(f)
}
func (node *Node) TraverseWithChannel() chan *Node {
out := make(chan *Node)
go func() {
node.TraverseFunc(func(node *Node) {
out <- node
})
close(out)
}()
return out
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2491/viewspace-2816036/ ,如需转载,请注明出处,否则将追究法律责任。