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

自定義類型

边栏推荐
- 本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
- AI 绘画极简教程
- Share an experience of self positioning + problem solving
- MySQL client tools are recommended. I can't imagine that it is best to use Juran
- AI painting minimalist tutorial
- VMware vSphere ESXi 7.0安装教程
- MySQL客户端工具推荐,一定想不到最好用巨然是它
- Go从入门到实战——Context与任务取消(笔记)
- Open a new ecological posture | use the wrodpress remote attachment to store it in COS
- 实际工作中用到的shell命令 - sed
猜你喜欢

Unity3d button adapts the size according to the text content

100 important knowledge points that SQL must master: using functions to process data

Codeforces Round #723 (Div. 2)

流程控制任务

Modify large online games through CE modifier

Focus! Tips for installing fonts on domestic computers

Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum

SQL必需掌握的100个重要知识点:排序检索数据

Flexible IP network test tool -- x-launch

银河麒麟系统局域网文件共享教程
随机推荐
Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
Educational Codeforces Round 108 (Rated for Div. 2)
1027 Colors in Mars
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
空指针异常
猜拳游戏专题训练
SQL必需掌握的100个重要知识点:检索数据
GoLand permanently activated
OpenSSL 编程 二:搭建 CA
麒麟V10安装字体
Serveur mandataire SQUID
MySQL Express - day 1 - basic introduction
Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!
Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum
Animal breeding production virtual simulation teaching system | Sinovel interactive
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
Acwing周赛57-最长连续子序列-(二分or树状数组)
Go从入门到实战——channel的关闭和广播(笔记)
1029 Median
Shell command used in actual work - sed