当前位置:网站首页>Go from introduction to practice - Interface (notes)
Go from introduction to practice - Interface (notes)
2022-06-27 21:37:00 【Accumulated Ytu】
Java Interface

Go Interface

type Mover interface {
move()
}
type dog struct {
}
func (d dog) move() {
fmt.Println(" The dog can move ")
}
func main() {
var x Mover
var wangcai = dog{
} // Wangcai is dog type
x = wangcai // x You can receive dog type
var fugui = &dog{
} // Wealth is *dog type
x = fugui // x You can receive *dog type
x.move()
}
From the above code, we can find , After implementing the interface with the value receiver , Whether it's dog Structure or structure pointer *dog All variables of type can be assigned to the interface variable . because Go There is a syntax sugar for evaluating pointer type variables ,dog The pointer fugui The interior will automatically evaluate *fugui
func (d *dog) move() {
fmt.Println(" The dog can move ")
}
func main() {
var x Mover
var wangcai = dog{
} // Wangcai is dog type
x = wangcai // x Can't accept dog type
var fugui = &dog{
} // Wealth is *dog type
x = fugui // x You can receive *dog type
}
At this time Mover The interface is *dog type , So we can't give x Pass in dog Type of wangcai, here x Can only store *dog Type value .
Interface variables

Custom type

边栏推荐
- 100 important knowledge points that SQL must master: retrieving data
- DO280OpenShift访问控制--security policy和章节实验
- Cortical traceability analysis of ERP
- 通过CE修改器修改大型网络游戏
- 农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
- 送你12个常用函数公式,用过的都说好
- Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
- 100 important knowledge points that SQL must master: combining where clauses
- VMware vSphere esxi 7.0 installation tutorial
- Full record of 2022 open source moment at Huawei partners and Developers Conference
猜你喜欢

BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

Go from entry to practice - dependency management (notes)

Save method of JPA stepping pit series

Codeforces Round #719 (Div. 3)

Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!

Focus! Tips for installing fonts on domestic computers

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen

Go from entry to practice - multiple selection and timeout control (notes)
随机推荐
Share how I take notes
Ceph分布式存储
Tiktok's interest in e-commerce has hit the traffic ceiling?
Go从入门到实战——所有任务完成(笔记)
SQL必需掌握的100个重要知识点:用通配符进行过滤
100 important knowledge points that SQL must master: filtering data
一套系统,减轻人流集中地10倍的通行压力
DO280OpenShift访问控制--security policy和章节实验
Codeforces Round #719 (Div. 3)
MySQL usage notes 1
小王的面试训练任务
GFS分布式文件系统
猜拳游戏专题训练
Open a new ecological posture | use the wrodpress remote attachment to store it in COS
Codeforces Round #723 (Div. 2)
MySQL客户端工具推荐,一定想不到最好用巨然是它
Educational Codeforces Round 108 (Rated for Div. 2)
Is it safe to open an account and buy stocks? Who knows
划重点!国产电脑上安装字体小技巧
白嫖红队goby&POC,叫你如何白嫖?