当前位置:网站首页>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
边栏推荐
- RT-1052学习笔记 - GPIO架构分析
- Thirty What are VM and VC?
- 一些接地气的话儿
- On IRP from the perspective of source code
- Leetcode 994, rotten orange
- Unity asset import settings
- Psychological formula for converting RGB to gray value
- Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
- [SQL] string series 2: split a string into multiple lines according to specific characters
- PHP的Laravel与Composer部署项目时常见问题
猜你喜欢
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
DOS command of Intranet penetration
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
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
[PTA] l1-002 printing hourglass
LeetCode 116. 填充每个节点的下一个右侧节点指针
Matlab matrix index problem
Leetcode 542, 01 matrix
MySQL基础之写表(创建表)
随机推荐
Implementation of mypromise
一. js的深拷贝和浅拷贝
Some basic knowledge of devexpress report development
Matlab matrix index problem
Leetcode 994, rotten orange
[PTA] get rid of singles
2021-06-29 C escape character cancellation and use
Case of the third day of go language development fresh every day project - news release system II
【PTA】L1-002 打印沙漏
Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
LeetCode 1346、检查整数及其两倍数是否存在
MySQL进阶之表的增删改查
Commande dos pour la pénétration de l'Intranet
go array
C# 知识
Leetcode 1351. Negative numbers in statistical ordered matrices
go struct
A useless confession artifact
Fastdfs思维导图
【栈和队列专题】—— 滑动窗口