当前位置:网站首页>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 string
Make 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 ?
边栏推荐
- The process of browser accessing the website
- Realize file download through the tag of < a > and customize the file name
- Shuttle + alluxio accelerated memory shuffle take-off
- 动态规划——相关概念,(数塔问题)
- leetcode435. Non overlapping interval
- Learn to use the idea breakpoint debugging tool
- 50 practical applications of R language (36) - data visualization from basic to advanced
- 1087 all roads lead to Rome (30 points)
- Market status and development prospect prediction of global fermented plant protein industry in 2022
- How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
猜你喜欢
MediaTek 2023 IC written examination approved in advance (topic)
Thesis reading_ Chinese NLP_ ELECTRA
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
Handler understands the record
Thesis reading_ Chinese medical model_ eHealth
MPM model and ab pressure test
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Review the old and know the new: Notes on Data Science
Shuttle + alluxio accelerated memory shuffle take-off
随机推荐
1086 tree traversals again (25 points)
"Pthread.h" not found problem encountered in compiling GCC
Keepalived热备与HAProxy
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
1118 birds in forest (25 points)
Shell script Basics - basic grammar knowledge
Analysis of proxy usage of ES6 new feature
M1 Pro install redis
Market status and development prospect prediction of the global fire extinguisher industry in 2022
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
Market status and development prospects of the global automatic tea picker industry in 2022
[luatos sensor] 2 air pressure bmp180
Market status and development prospect forecast of global heat curing adhesive industry in 2022
Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
Market status and development prospects of the global IOT active infrared sensor industry in 2022
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
1119 pre- and post order traversals (30 points)
Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time