• 每个目录一个包
  • main包包含可执行入口
  • 为结构定义的方法必须放在同一个包内
  • 为结构定义的方法必须放在同一个包内
  • 可以是不同文件

node.go

  1. package tree
  2. import "fmt"
  3. type TreeNode struct {
  4. Value int
  5. Left, Right *TreeNode
  6. }
  7. func (node TreeNode) Print() {
  8. fmt.Print(node.Value)
  9. fmt.Print(" ")
  10. }
  11. func (node *TreeNode) SetValue(value int) {
  12. if node == nil {
  13. fmt.Println("setting value to nil node,ignored")
  14. }
  15. node.Value = value
  16. }
  17. func (node *TreeNode) Traverse() {
  18. if node == nil {
  19. return
  20. }
  21. node.Left.Traverse()
  22. node.Print()
  23. node.Right.Traverse()
  24. }
  25. func createNode(value int) *TreeNode {
  26. return &TreeNode{Value: value}
  27. }

entry.go

  1. package main
  2. import "go_code/project04/tree"
  3. func main() {
  4. var root tree.TreeNode
  5. // fmt.Println(root)
  6. root = tree.TreeNode{Value: 3}
  7. root.Left = &tree.TreeNode{}
  8. root.Right = &tree.TreeNode{5, nil, nil}
  9. root.Right.Left = new(tree.TreeNode)
  10. root.Traverse()
  11. }

目录结构
image.png

如何扩充系统类型或者别人的类型

  • 定义别名 ```go package main

import “go_code/project04/tree” import “fmt”

type myTreeNode struct { node *tree.TreeNode }

func (myNode *myTreeNode) postOrder() { if myNode == nil || myNode.node == nil{ return }

  1. left := myTreeNode{myNode.node.Left}
  2. left.postOrder()
  3. right := myTreeNode{myNode.node.Right}
  4. right.postOrder()
  5. myNode.node.Print()

}

func main() { var root tree.TreeNode // fmt.Println(root) root = tree.TreeNode{Value: 3} root.Left = &tree.TreeNode{} root.Right = &tree.TreeNode{5, nil, nil} root.Right.Left = new(tree.TreeNode)

  1. root.Traverse()
  2. fmt.Println()
  3. myRoot := myTreeNode{&root}
  4. myRoot.postOrder()
  5. fmt.Println()

}

  1. - 使用组合
  2. ```go
  3. package queue
  4. type Queue []int
  5. func (q *Queue) Push(v int) {
  6. *q = append(*q, v)
  7. }
  8. func (q *Queue) Pop() int {
  9. head := (*q)[0]
  10. *q = (*q)[1:]
  11. return head
  12. }
  13. func (q *Queue) IsEmpty() bool {
  14. return len(*q) == 0
  15. }
  1. package main
  2. import (
  3. "go_code/project04/queue"
  4. "fmt"
  5. )
  6. func main() {
  7. q := queue.Queue{1}
  8. q.Push(2)
  9. q.Push(3)
  10. fmt.Println(q.Pop())
  11. fmt.Println(q.Pop())
  12. fmt.Println(q.IsEmpty())
  13. fmt.Println(q.Pop())
  14. fmt.Println(q.IsEmpty())
  15. }
  • 使用内嵌 ```go package main

import “go_code/project04/tree” import “fmt”

type myTreeNode struct { *tree.TreeNode //Embedding 内嵌 }

func (myNode *myTreeNode) postOrder() { if myNode == nil || myNode.TreeNode == nil{ return }

  1. left := myTreeNode{myNode.Left}
  2. left.postOrder()
  3. right := myTreeNode{myNode.Right}
  4. right.postOrder()
  5. myNode.Print()

}

func (myNode *myTreeNode) Traverse() { fmt.Println(“this method is shadowed “) }

func main() { // var root tree.TreeNode // fmt.Println(root) root := myTreeNode{&tree.TreeNode{Value: 3}} root.Left = &tree.TreeNode{} root.Right = &tree.TreeNode{5, nil, nil} root.Right.Left = new(tree.TreeNode)

  1. root.Traverse()
  2. fmt.Println()
  3. root.TreeNode.Traverse()
  4. fmt.Println()
  5. root.postOrder()
  6. fmt.Println()
  7. // myRoot := myTreeNode{&root}
  8. // myRoot.postOrder()
  9. // fmt.Println()

} ```