当前位置:网站首页>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

边栏推荐
- MySQL performance optimization index function, hidden, prefix, hash index usage (2)
- 小王的面试训练任务
- ABC-Teleporter Setting-(思维+最短路)
- Go from entry to practice - dependency management (notes)
- How to participate in openharmony code contribution
- Tutorial | fNIRS data processing toolkit homer2 download and installation
- This is the same as data collection. Can you define a parameter as last month or the previous day, and then use this parameter in SQL?
- 专题教程——选队长游戏
- Cortical traceability analysis of ERP
- Shell script controls the startup and shutdown of services - with detailed cases
猜你喜欢

Serveur mandataire SQUID

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!"

mysql使用笔记一

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

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

微服务之远程调用

释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束

Can Oracle's CTAs bring constraints and other attributes to the new table?

A set of system to reduce 10 times the traffic pressure in crowded areas

展现强劲产品综合实力 ,2022 款林肯飞行家Aviator西南首秀
随机推荐
数组作业题
微服务之远程调用
Flask----应用案例
如何将队列里面的内容没秒钟执行一次优先级
Acwing周赛57-最长连续子序列-(二分or树状数组)
Educational Codeforces Round 108 (Rated for Div. 2)
Safe and efficient, non-contact "hand brushing" identification helps epidemic prevention and control
GoLand permanently activated
Oracle的CTAS能不能将约束等属性带到新表?
SQL必需掌握的100个重要知识点:创建计算字段
Day8 ---- 云资讯项目介绍与创建
快递e栈——数组篇小型项目
Shell script controls the startup and shutdown of services - with detailed cases
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
Codeforces Round #719 (Div. 3)
Go从入门到实战——Context与任务取消(笔记)
OpenSSL 编程 一:基本概念
JPA踩坑系列之save方法
系统自带的karsonzhang/fastadmin-addons报错