当前位置:网站首页>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類型的值。
接口變量

自定義類型

边栏推荐
- Can Oracle's CTAs bring constraints and other attributes to the new table?
- GFS分布式文件系统
- 互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
- A set of system to reduce 10 times the traffic pressure in crowded areas
- KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
- 如何将队列里面的内容没秒钟执行一次优先级
- 数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
- Kirin V10 installation font
- 分享一次自己定位 + 解决问题的经历
- White whoring red team goby & POC, how do you call white whoring?
猜你喜欢

ICML2022 | 可扩展深度高斯马尔可夫随机场

麒麟V10安装字体

Codeforces Round #719 (Div. 3)

Go from entry to practice - dependency management (notes)

流程控制任务

GFS分布式文件系统

White whoring red team goby & POC, how do you call white whoring?

After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"

行业案例|从零售之王看银行数字化转型的运营之道

强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
随机推荐
抖音的兴趣电商已经碰到流量天花板?
使用storcli工具配置RAID,收藏这一篇就够了
100 important knowledge points for SQL: in operator
Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
Codeforces Round #722 (Div. 2)
Acwing周赛57-最长连续子序列-(二分or树状数组)
Educational Codeforces Round 108 (Rated for Div. 2)
Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
Here are 12 commonly used function formulas for you. All used ones are good
覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎
Share an experience of self positioning + problem solving
Go从入门到实战——所有任务完成(笔记)
集合代码练习
Scrum和看板的区别
系统自带的karsonzhang/fastadmin-addons报错
OpenSSL 编程 一:基本概念
Let Ma Huateng down! Web3.0, hopeless
Shell command used in actual work - sed
Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China