当前位置:网站首页>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 .
边栏推荐
- Can‘t connect to server on ‘IP‘ (60)
- 'global event bus' &' message subscription and Publishing '
- Js继承方法
- I am a 27 year old technical manager, whose income is too high, and my heart is in a panic
- 第2章-2 计算分段函数[1]
- JS inheritance method
- Vrrp+mstp configuration details [Huawei ENSP experiment]
- Round C financing has been completed! Smart software leads domestic Bi ecological empowerment, and products and services are a step forward
- 1299_ Task status and switching test in FreeRTOS
- The five pictures tell you: why is there such a big gap between people in the workplace?
猜你喜欢

Alibaba technology has four sides + intersection +hr, and successfully got the offer. Can't double non undergraduate students enter the big factory?

Data fabric, next air outlet?

XMIND Zen installation tutorial

There is a bug in installing CONDA environment

Flink window & time principle

博客搭建九:hugo添加搜索功能

Js继承方法

思迈特软件完成C轮融资,让BI真正实现“普惠化”
![Chapter 2-2 calculation of piecewise function [1]](/img/40/cad6bf92849624199af0fd1ba1d433.jpg)
Chapter 2-2 calculation of piecewise function [1]

1w5 words to introduce those technical solutions of distributed system in detail
随机推荐
What content does the new version of network security level protection evaluation report template contain? Where can I find it?
Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice
C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running
Blog Building 9: add search function to Hugo
Data fabric, next air outlet?
Gbase 8A MPP and Galaxy Kirin (x86 version) complete deep adaptation
How to configure phpunit under window
客户至上 | 国产BI领跑者,思迈特软件完成C轮融资
JS手写函数之slice函数(彻底弄懂包头不包尾)
Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]
博客搭建七:hugo
What are the main uses of digital factory management system
1w5字详细介绍分布式系统的那些技术方案
Detailed explanation of switch link aggregation [Huawei ENSP]
I use SqlClient normally, and dlink submission will report this error. What should I do?
This flick SQL timestamp_ Can ltz be used in create DDL
SQL server time field sorting
Explain cache consistency and memory barrier
Hundreds of billions of it operation and maintenance market has come to the era of speaking by "effect"
分布式系统架构理论与组件