当前位置:网站首页>Go interface advanced
Go interface advanced
2022-07-28 08:54:00 【go|Python】
List of articles
go Interface advanced
Value receiver and pointer receiver
In the basic part of the interface , We use value receivers to implement interfaces . Again , You can also use pointer receivers to implement interfaces , However, when using the pointer receiver to implement the interface , There are also some details to pay attention to .
func main() {
tduck := TDuck{
" Donald Duck ", 3}
pduck := PDuck{
" Duckling ", 2}
// Whether it is the interface implemented by the value receiver or the interface implemented by the pointer receiver , Values can be called
tduck.eat() // Donald Duck Eat something
pduck.eat() // Duckling Eat something
// Whether it is the interface implemented by the value receiver or the interface implemented by the pointer receiver , Pointers can be used to call
tduck1.eat() // Donald Duck Eat something
pduck1.eat() // Duckling Eat something
// Use the interface type to call
// The interface implemented by the value receiver , Values and pointers can call
var duck Duck = tduck
var duck1 Duck = &tduck
duck.eat() // Donald Duck Eat something
duck1.eat() // Donald Duck Eat something
// If it is the interface implemented by the pointer receiver , You cannot directly give the value to the interface , You need to assign the pointer to the interface type
// var duck2 Duck = pduck Report errors
var duck3 Duck = &pduck
duck3.eat() // Duckling Eat something
}
type Duck interface {
eat()
}
type TDuck struct {
name string
age int
}
type PDuck struct {
name string
age int
}
// Value recipient
func (t TDuck) eat() {
fmt.Println(t.name, " Eat something ")
}
// Pointer receiver
func (t *PDuck) eat() {
fmt.Println(t.name, " Eat something ")
}
Implement multiple interfaces
Types can implement multiple interfaces .
func main() {
tduck := TDuck{
" Donald Duck 1 Number ",5}
var animal Animal = tduck
var duck Duck = tduck
tduck.run() // Donald Duck 1 Number Rattle and run
tduck.eat() // Donald Duck 1 Number Eat something
// be not in Animal1 Methods in interfaces , You can't get it , Through type assertion , Type selection
animal.run() // Donald Duck 1 Number Rattle and run
duck.eat() // Donald Duck 1 Number Eat something
}
type Animal interface {
run()
}
type Duck interface {
eat()
}
type TDuck struct {
name string
age int
}
func (t TDuck) eat() {
fmt.Println(t.name, " Eat something ")
}
func (t TDuck) run() {
fmt.Println(t.name, " Rattle and run ")
}
type TDuck At the same time Animal and Duck Interface , therefore TDuck An object of type can have all the implemented methods and properties , The interface object can only execute its corresponding method .
Also because of this feature , Different functions , The types that can be received are different , But they can all pass the same specific type , That must be because of this specific type , Implemented multiple interfaces
Nesting of interfaces
Even though Go The language does not provide an inheritance mechanism , However, other interfaces can be nested , Create a new interface .
func main() {
nb := Nb{
"lxx",2}
var man Man = nb
var woman Woman = nb
var people People = nb
man.makeMoney() // Earning skills
woman.spendMoney() // Spending skills
fmt.Println(people) // {lxx 2}
}
type Man interface {
makeMoney()
}
type Woman interface {
spendMoney()
}
type People interface {
Man
Woman
}
type Nb struct {
name string
age int
}
func (n Nb) makeMoney() {
fmt.Println(" Earning skills ")
}
func (n Nb) spendMoney() {
fmt.Println(" Spending skills ")
}
As shown in the above code ,Nb People can both makeMoney It will be spendMoney,makeMoney yes Man Interface method ,spendMoney yes Woman Interface method ,Nb Type implements two interfaces at the same time , It will come true People Interface .
边栏推荐
- Simple use of unity queue
- Hcip day 8
- 1w5 words to introduce those technical solutions of distributed system in detail
- Slice function of JS handwriting function (thoroughly understand the header but not the footer)
- 谷歌 Material Design 的文本框为什么没人用?
- 第2章-14 求整数段和
- How to execute the SQL assembled in ODPs SQL function and get the return value?
- Completion report of communication software development and Application
- Why setting application.targetframerate doesn't work
- Redis 基本知识,快来回顾一下
猜你喜欢

1299_ Task status and switching test in FreeRTOS

5张图告诉你:同样是职场人,差距怎么这么大?

Flink window & time principle

招贤纳士,GBASE高端人才招募进行中

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running

中标捷报!南大通用GBase 8s中标南瑞集团2022年数据库框架项目

阿里巴巴内部面试资料

Win the bid! Nantah general gbase 8s won the bid for the 2022 database framework project of NARI Group

SQL Server查询结果导出到EXCEL表格
随机推荐
Competition: diabetes genetic risk detection challenge (iFLYTEK)
Bash shell interaction free
Mobaxtermsession synchronization
Why can ThreadLocal achieve thread isolation?
When will brain like intelligence, which is popular in academia, land? Let's listen to what the industry masters say - qubits, colliders, x-knowledge Technology
Three ways to create threads
SQL Server查询结果导出到EXCEL表格
XMIND Zen installation tutorial
思迈特软件完成C轮融资,让BI真正实现“普惠化”
PHP基础知识 - PHP 使用 MySQLI
创建线程的3种方式
Flink window & time principle
NDK 系列(6):说一下注册 JNI 函数的方式和时机
[cloud computing] several mistakes that enterprises need to avoid after going to the cloud
The current value of uniapp's swiper dynamic setting does not take effect solution
How to import and export Youxuan database
Eight ways to solve EMC and EMI conducted interference
How to configure phpunit under window
GBASE亮相联通云巡展(四川站) 以专业赋能云生态
Explain cache consistency and memory barrier