golang 结构体

code

package main

import "fmt"

//go 语言仅支持封装,不支持继承和多态
//继承和多态通过接口实现
//go 只有 struct,没有 class
type treeNode struct {
    value       int
    left, right *treeNode
}

func main() {
    var root treeNode
    root = treeNode{value: 3}
    root.left = &treeNode{}
    root.right = &treeNode{5, nil, nil}
    root.right.left = new(treeNode)

    nodes := []treeNode{
        {value: 3},
        {},
        {6, nil, &root},
    }
    fmt.Println(nodes)
}

result

[{3 <nil> <nil>} {0 <nil> <nil>} {6 <nil> 0xc00005e420}]

添加新评论