package main

type Data struct{}

func (Data) TestValue() {}

func (*Data) TestPointer() {}

func main() { var p *Data = nil p.TestPointer()

(*Data)(nil).TestPointer() // method value
(*Data).TestPointer(nil)   // method expression

// p.TestValue()            // invalid memory address or nil pointer dereference

// (Data)(nil).TestValue()  // cannot convert nil to type Data
// Data.TestValue(nil)      // cannot use nil as type Data in function argument

}

为什么能这么用? (Data)(nil).TestPointer() , 第一个Data 我不懂, 我知道强制类型转换是 T(), 就相当于只有*Data(nil), 但是为啥又多套了一个(), 啥原理啊, 抱歉实在不知道怎么谷歌, go 小白

举报· 316 次点击
登录 注册 站外分享
2 条回复  
XiaoXiaoMagician 小成 2025-3-12 11:29:21
这种情况建议把如何使用 AI 纳入学习
GeruzoniAnsasu 小成 2025-3-12 11:32:17
复制粘贴 AI 的回答会引爆账号,所以 > claude.ai: > 我对 golang 的语法感到疑惑,帮我逐字逐句分析一下 demo 里的各个表达式是什么意思,怎么构成的 自己看
返回顶部