例如,nodes
作为指向 的指针切片Node
,
package main
import (
"fmt"
"net"
)
type Node struct {
value int
ip net.IP
nodes []*Node
}
func main() {
node1 := Node{value: 1}
node2 := Node{value: 2}
node3 := Node{value: 3}
node4 := Node{value: 4}
node1.nodes = append(node1.nodes, &node2, &node3)
node2.nodes = append(node2.nodes, &node4)
node3.nodes = append(node3.nodes, &node4)
fmt.Printf("node1: %p %v\n", &node1, node1)
fmt.Printf("node2: %p %v\n", &node2, node2)
fmt.Printf("node3: %p %v\n", &node3, node3)
fmt.Printf("node4: %p %v\n", &node4, node4)
}
输出:
node1: 0xc200069100 {1 [] [0xc200069180 0xc200069200]}
node2: 0xc200069180 {2 [] [0xc200069240]}
node3: 0xc200069200 {3 [] [0xc200069240]}
node4: 0xc200069240 {4 [] []}
切片类型
追加和复制切片
切片技巧
指针类型