当前位置:网站首页>Go从入门到实战——接口(笔记)
Go从入门到实战——接口(笔记)
2022-06-27 19:22: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类型的值。
接口变量

自定义类型

边栏推荐
- MySQL performance optimization index function, hidden, prefix, hash index usage (2)
- Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
- TypeScript学习
- 安装gatewayworker之后启动start.php
- 100 important knowledge points for SQL: in operator
- oss上传调用的是哪个方法
- shell脚本控制服务的启动和关闭 - 具备详细案例
- 事件相关电位ERP的皮层溯源分析
- Focus! Tips for installing fonts on domestic computers
- 行业案例|从零售之王看银行数字化转型的运营之道
猜你喜欢

Educational Codeforces Round 108 (Rated for Div. 2)

Codeforces Round #719 (Div. 3)

华为伙伴暨开发者大会2022开源时刻全纪录

Codeforces Global Round 14

Use the storcli tool to configure raid. Just collect this article

猜拳游戏专题训练

01-Golang-环境搭建

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

Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China

非常全面的DolphinScheduler(海豚调度)安装使用文档
随机推荐
系统自带的karsonzhang/fastadmin-addons报错
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
A set of system to reduce 10 times the traffic pressure in crowded areas
GoLand永久激活
SQL Server for循环用法
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
GFS分布式文件系统
2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
100 important knowledge points that SQL must master: combining where clauses
SQL必需掌握的100个重要知识点:创建计算字段
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
AI 绘画极简教程
Codeforces Round #719 (Div. 3)
Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
Can Oracle's CTAs bring constraints and other attributes to the new table?
OpenSSL 编程 一:基本概念
非常全面的DolphinScheduler(海豚调度)安装使用文档
猜拳游戏专题训练
Focus! Tips for installing fonts on domestic computers