当前位置:网站首页>Go從入門到實戰——接口(筆記)
Go從入門到實戰——接口(筆記)
2022-06-27 21:37:00 【積澱 ytu】
Java接口
Go接口
type Mover interface {
move()
}
type dog struct {
}
func (d dog) move() {
fmt.Println("狗會動")
}
func main() {
var x Mover
var wangcai = dog{
} // 旺財是dog類型
x = wangcai // x可以接收dog類型
var fugui = &dog{
} // 富貴是*dog類型
x = fugui // x可以接收*dog類型
x.move()
}
從上面的代碼中我們可以發現,使用值接收者實現接口之後,不管是dog結構體還是結構體指針*dog類型的變量都可以賦值給該接口變量。因為Go語言中有對指針類型變量求值的語法糖,dog指針 fugui
內部會自動求值*fugui
func (d *dog) move() {
fmt.Println("狗會動")
}
func main() {
var x Mover
var wangcai = dog{
} // 旺財是dog類型
x = wangcai // x不可以接收dog類型
var fugui = &dog{
} // 富貴是*dog類型
x = fugui // x可以接收*dog類型
}
此時實現Mover接口的是*dog
類型,所以不能給x傳入dog類型的wangcai,此時x只能存儲*dog
類型的值。
接口變量
自定義類型
边栏推荐
- White whoring red team goby & POC, how do you call white whoring?
- 快递e栈——数组篇小型项目
- SQL必需掌握的100个重要知识点:排序检索数据
- Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
- Go从入门到实战——Context与任务取消(笔记)
- uniapp拦截请求
- Go从入门到实战—— 多路选择和超时控制(笔记)
- BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
- Is it safe to open an account and buy stocks? Who knows
- 创建对象时JVM内存结构
猜你喜欢
Industry case | see the operation of bank digital transformation from the king of retail
SQL必需掌握的100个重要知识点:使用函数处理数据
体验Navicat Premium 16,无限重置试用14天方法(附源码)
抖音的兴趣电商已经碰到流量天花板?
划重点!国产电脑上安装字体小技巧
GoLand permanently activated
Focus! Tips for installing fonts on domestic computers
2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
Educational Codeforces Round 108 (Rated for Div. 2)
How to do a good job of gateway high availability protection in the big promotion scenario
随机推荐
Go从入门到实战——Context与任务取消(笔记)
oss上传调用的是哪个方法
通过CE修改器修改大型网络游戏
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
Share an experience of self positioning + problem solving
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
Zhongang Mining: the largest application field of new energy or fluorite
uniapp拦截请求
送你12个常用函数公式,用过的都说好
划重点!国产电脑上安装字体小技巧
Codeforces Round #717 (Div. 2)
"Good voice" has been singing for 10 years. How can the Chinese language in the starry sky sing well in HKEx?
SQL必需掌握的100个重要知识点:检索数据
Tutorial | fNIRS data processing toolkit homer2 download and installation
专题教程——选队长游戏
PCIE知识点-008:PCIE switch的结构
squid代理服務器
Codeforces Round #719 (Div. 3)
Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
GFS distributed file system