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

自定义类型

边栏推荐
- 创建对象时JVM内存结构
- Go从入门到实战—— 多路选择和超时控制(笔记)
- SQL必需掌握的100个重要知识点:排序检索数据
- squid代理服務器
- mime. Type file content
- 体验Navicat Premium 16,无限重置试用14天方法(附源码)
- 释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
- Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
- Industry case | see the operation of bank digital transformation from the king of retail
- Share an experience of self positioning + problem solving
猜你喜欢

100 important knowledge points that SQL must master: filtering data

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

一套系统,减轻人流集中地10倍的通行压力

让马化腾失望了!Web3.0,毫无希望

Codeforces Round #723 (Div. 2)

送你12个常用函数公式,用过的都说好

Full record of 2022 open source moment at Huawei partners and Developers Conference

Squid proxy server

Serveur mandataire SQUID

Oracle的CTAS能不能将约束等属性带到新表?
随机推荐
Industry case | see the operation of bank digital transformation from the king of retail
SQL server for circular usage
01-Golang-环境搭建
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
教程|fNIRS数据处理工具包Homer2下载与安装
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
MySQL客户端工具推荐,一定想不到最好用巨然是它
Prospects for enterprise digitalization (38/100)
白嫖红队goby&POC,叫你如何白嫖?
ARCS模型介绍
安装gatewayworker之后启动start.php
Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)
空指针异常
关于企业数字化的展望(38/100)
Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
How to do a good job of gateway high availability protection in the big promotion scenario
释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
如何将队列里面的内容没秒钟执行一次优先级
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
MySQL Express - day 1 - basic introduction