語法易錯(cuò)點(diǎn)
- 傳結(jié)構(gòu)的值時(shí),結(jié)構(gòu)體的對(duì)象類型會(huì)被轉(zhuǎn)換為接收者定義的類型
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}},
}
- 值在使用時(shí)要初始化,不然會(huì)報(bào)訪問nil對(duì)象
runtime error: invalid memory address or nil pointer dereference\ngoroutine
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。