当前位置:网站首页>go interface
go interface
2022-04-23 20:43:00 【baboon_ chen】
One 、 Definition of interface
An interface is a collection of one or more method signatures , As long as any type has all the methods corresponding to this , It means it's Realization
The interface .
- Pick up ⼝ Naming habits with
er
ending , Structure .- Pick up ⼝ Only method signature , It didn't come true .
- Pick up ⼝ No data fields .
- But you can pick up ⼝ Inlay ⼊ Other connections
- Type can implement multiple connections ⼝.
Two 、 Empty interface
Air connection ⼝
interface{}
There is no ⽅ Legal signature , This means that any type implements null connection ⼝. therefore , An empty interface can store any type of data .
3、 ... and 、 The execution mechanism of the interface
Pick up ⼝ Object by
Pick up ⼝ surface (interface table) The pointer
andData pointer
form .
runtiem.h
struct Iface
{
Itab* tab;
void* data;
};
struct Itab
{
InterfaceType* inter;
Type* type;
void (*fun[])(void);
};
The interface value is a two word data structure , The first word contains a pointer to the internal table . This internal table is called iTable
, Contains the type information of the stored value .iTable
Contains the type information of the stored value and a set of methods associated with the value . The second word is a Pointer to the stored value .
Sketch of interface value after entity value assignment :
Sketch of interface value after entity pointer assignment :
Four 、 Interface conversion
1、 Types of assertions
func test1(i interface{
}) {
if v, ok := i.(int); ok {
fmt.Printf("type is int, value: %v", v)
}
}
2、switch Make type batch judgment , I won't support it fallthrough
.
func test2(i interface{
}) {
switch v := i.(type) {
case nil:
fmt.Println("this is nil")
case int:
fmt.Println("this is int: ", v)
case string:
fmt.Println("this is string: ", v)
case float32:
fmt.Println("this is float32: ", v)
default:
fmt.Println("unknow type")
}
}
3、 A superset interface object can be converted to a subset interface , And vice versa .
type Peopler interface {
Study()
Animal
}
type Animal interface {
Eating()
}
type Student struct {
Name string
}
func (s Student) Study() {
fmt.Println("I am study.")
}
func (s Student) Eating() {
fmt.Println("I am Eating.")
}
func test3() {
var p Peopler = Student{
Name: "XiaoMing"}
var a Animal = p
a.Eating()
}
5、 ... and 、 Let the function implement the interface
type Tester interface {
Do()
}
type FuncDo func()
func (self FuncDo) Do() {
self() }
func test4() {
var f Tester = FuncDo(func() {
fmt.Println("hello world!") })
f.Do()
}
版权声明
本文为[baboon_ chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210546351237.html
边栏推荐
- I JS deep copy and shallow copy
- BMP JPEG picture to vector image contourtrace
- Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
- MySQL数据库常识之储存引擎
- 【栈和队列专题】—— 滑动窗口
- Plato Farm元宇宙IEO上线四大,链上交易颇高
- Unity Odin ProgressBar add value column
- An error occurs when the addressable assets system project is packaged. Runtimedata is null
- Vscode download speed up
- Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
猜你喜欢
vulnhub DC:1渗透笔记
DOS command of Intranet penetration
Resolve the eslint warning -- ignore the warning that there is no space between the method name and ()
【PTA】整除光棍
Recognition of high-speed road signs by Matlab using alexnet
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
[matlab 2016 use mex command to find editor visual studio 2019]
Go zero framework database avoidance Guide
go slice
Automatically fill in body temperature and win10 task plan
随机推荐
C# 知识
LeetCode 232、用栈实现队列
go map
[PTA] l1-002 printing hourglass
Automatically fill in body temperature and win10 task plan
Analysis of the relationship between generalized Bim and CAD under the current background
How to use PM2 management application? Come in and see
pikachuxss如何获取cookie靶场,返回首页总是失败
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
LeetCode-279-完全平方数
Case of the third day of go language development fresh every day project - news release system II
上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案
缓存淘汰算法初步认识(LRU和LFU)
Go限制深度遍历目录下文件
Implementation of mypromise
MySQL数据库常识之储存引擎
MySQL 存储过程和函数
bounding box iou
Solution: NPM err! code ELIFECYCLE npm ERR! errno 1
三十一. `prototype`显示原型属性和`__proto__`隐式原型属性