当前位置:网站首页>Go language interface learning notes
Go language interface learning notes
2022-07-03 05:03:00 【CorGi_ eight thousand four hundred and fifty-six】
Preface
Studying recently Go Language , See the interface . Because I was engaged in Python, It advocates object-oriented programming . and Go The language is somewhat different ,Go It advocates interface oriented programming , therefore Go Language interface is particularly important .
I just learned Go Language soon , I want to write my understanding of the interface , If there are mistakes or better understanding methods , Please correct me .
One 、 What is an interface
An interface is an abstract type , Is a collection of methods in code . Interfaces are like defining the code of conduct for methods , Whatever your type , Whatever you can achieve .【 If you “ Quack quack ” It's called , Then you are the duck (?)】
Two 、 Interface implementation
The most basic interface consists of interface Type and one or more methods
2.1 Interface declaration
type The interface name interface {
Method 1( Parameters ) Return value
Method 2( Parameters ) Return value
Method 3( Parameters ) Return value
}2.2 Method statement
Go The method is a function with a specific receiver .Go The function in is defined by Function name , Parameters , Return value , The body of the function constitute , Method receives more values on the basis of function :
func ( The receiver , Receiver type ) Method name ( Receiving parameters ) ( Return value ){
The body of the function
} 2.3 How to implement the interface
for instance , The following defines an interface called buyer, There's a buy Methods
type buyer interface {
buy()
}
Define a string type
type Microsoft stringMake the string type pass buyer Interface call buy Method
// Realized buy Interface , Print
func (m Microsoft) buy() {
fmt.Println("Activision and Blizzard is mine")
}
func main() {
// Make a statement buyer Interface xbox
var xbox buyer
// Instantiation Microsoft to xbox
xbox = Microsoft("I'm rich")
// Calling method
xbox.buy()
}
The whole process : First define a Microsoft , Let me define one more buy The way to acquire Activision Blizzard , Then declare a buyer It's called xbox, Then instantiate Microsoft to pay xbox, As a buyer xbox To realize buy Methods , Buy Activision Blizzard .( Rub a hot spot )
Blind summary
An essay with a little idea suddenly , The above is just a little bit of my superficial understanding , If you show timidity, please correct !
Interface nesting , Empty interface, etc. I'll digest it well and then continue to write , Suddenly switch to Go Language acceptance is not very good , It's still too much .
Is it a duck that quacks ?
边栏推荐
- [Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
- Thesis reading_ Chinese NLP_ ELECTRA
- Market status and development prospect prediction of global colorimetric cup cover industry in 2022
- Wechat applet waterfall flow and pull up to the bottom
- My first Smartphone
- @RequestMapping
- [set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
- Market status and development prospect prediction of global fermentation acid industry in 2022
- 1110 complete binary tree (25 points)
- JDBC database operation
猜你喜欢

"Hands on deep learning" pytorch edition Chapter II exercise
![[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)](/img/c2/87358af6b2b2892a6eceb751b3b60c.jpg)
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)

Thesis reading_ ICD code_ MSMN

Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis

MediaTek 2023 IC written examination approved in advance (topic)

leetcode860. Lemonade change

Shuttle + alluxio accelerated memory shuffle take-off

Keepalived热备与HAProxy

论文阅读_中文医疗模型_ eHealth

Review the old and know the new: Notes on Data Science
随机推荐
String matching: find a substring in a string
[research materials] 2021 China's game industry brand report - Download attached
Market status and development prospect forecast of global heat curing adhesive industry in 2022
leetcode406. Rebuild the queue based on height
Shuttle + Alluxio 加速内存Shuffle起飞
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip
[XSS bypass - protection strategy] understand the protection strategy and better bypass
What is UUID
Celebrate the new year together
Notes | numpy-11 Array operation
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
The 19th Zhejiang I. barbecue
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
Wechat applet waterfall flow and pull up to the bottom
How to connect the network: Chapter 1 CSDN creation punch in
Career planning of counter attacking College Students
1111 online map (30 points)