go struct的嵌套/组合以及interface
2018-11-16 19:39
337 查看
版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权。 https://blog.csdn.net/stpeace/article/details/84136563
看代码:
[code]package main import "fmt" type Intf interface { process() } type MsgBase struct { id int } func (p *MsgBase) process() { fmt.Printf("base %v\n", p) } type Msg1 struct { MsgBase x int } type Msg2 struct { MsgBase x int y int } func (p *Msg1) process() { fmt.Printf("business %v\n", p) } func main() { var m Intf = new(Msg1) // 不能用var m MsgBase = new(Msg1) m.process() m = new(Msg2) m.process() }
结果:
business &{{0} 0}
base &{0}
不多收。
阅读更多
相关文章推荐
- sizeof求struct各种组合的长度,以及指针,字符数组的长度
- 阅读go项目源码,解决无法快速找到interface有哪些struct实现问题
- Go语言的接口interface、struct和组合、继承
- 【Go语言社区】接受 interface 参数,返回 struct 在 go 中意味着什么
- 翻译-接受 interface 参数,返回 struct 在 go 中意味着什么
- class、interface、struct的差别
- go_组合接口
- Linux下goahead3.3.6的移植以及文件上传下载示例
- 测试Go语言的interface的效率
- must,must_not,should组合关系以及OR和AND
- 5种方式将机器学习带到Java、Python以及Go等编程语言
- [初学笔记] matlab中 struct的用法,以及如何保存在xls中
- Go语言学习笔记(四)结构体struct & 接口Interface & 反射reflect
- 排列组合问题以及其应用
- Go语言学习之数据类型以及类型转换(The way to go)
- c struct 以及与之相关的一些其他小问题
- Effective Go -> Interface
- Go 学习笔记:Println 与 Printf 的区别,以及 Printf 的详细用法
- class和struct的区别以及类的内存结构
- interface学习以及 抽象类和接口的异同点