语法易错点
- 传结构的值时,结构体的对象类型会被转换为接收者定义的类型
dataList := []struct {
dst interface{}
src interface{}
want interface{}
}{
//{PersonM{Name: "Alice", Age: 20}, PersonM{Age: 20, Name: "Alice"}, PersonM{Name: "Alice", Age: 20}},
//{PersonM{Name: "Alice", Age: 20}, PersonM{Name: "Bob", Age: 20}, PersonM{Name: "Bob", Age: 20}},
{&PersonM{Name: "Alice", Age: 20}, PersonM{Name: "Bob", Age: 21}, PersonM{Name: "Bob", Age: 21}},
//{WomanM{Name: "Alice", Age: 20}, PersonM{Name: "Alice", Age: 20}, WomanM{Name: "Alice", Age: 20}},
//{WomanM{Name: "Alice", Age: 20}, PersonM{Name: "Alice", Age: 20, Height: 170}, WomanM{Name: "Alice", Age: 20}},
//{PersonM{Name: "Alice", Age: 20, Height: 170}, WomanM{Name: "Alice", Age: 21}, PersonM{Name: "Alice", Age: 21, Height: 170}},
//{PersonM{Name: "Alice"}, WomanM{Name: "Bob", Age: 21}, PersonM{Name: "Bob", Age: 21}},
}
- 值在使用时要初始化,不然会报访问nil对象
runtime error: invalid memory address or nil pointer dereference\ngoroutine
网友评论