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 小白
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.