Go: 4-1 面向对象

Go语言的面向对象

结构的创建
 type treeNode struct {
 	value int
 	left,right *treeNode
 }
 
 func CreatNode(value int)*treeNode  {
 	return &treeNode{value:value}
 }
 
 root := treeNode{value:3}
 root.right = CreatNode(3)
type struct T { xxx}
func f() *T {
    var ret T
    return &ret
}
	
const Width, Height = 640, 480
type Cursor struct {
    X, Y int
}

func Center(c *Cursor) {
    c.X += Width / 2
    c.Y += Height / 2
}

func CenterCursor() {
    c := new(Cursor)
    Center(c)
    fmt.Println(c.X, c.Y)
}

值接收者 VS 指针接收者

封装

扩充系统类型或者第三方库类型