go interface 使用

简介: go interface 使用

初学者可以 简单的将 interface当做一种 可变的数据类型

即 所有实现了 interface 所有方法的函数都可以 保存到 interface 中,

由于 interface{} 一个函数都没有实现,所以所有的函数都可以用 interface{} 类型来表示


类似 c 的void *

嵌入 interface

类似结构体的引用其他结构体

接口也可以引用其他的接口

type heap  interface {
        sort.Interface //嵌入字段sort.Interface
        Push(x interface{}) //a Push method to push elements into the heap
        Pop() interface{} //a Pop elements that pops elements from the heap
}


io.ReadWriter 中的实际代码。 即 接口 ReadWriter 由 Reader Writer 构成

// io.ReadWriter
type ReadWriter interface {
        Reader
        Writer
}







相关文章
|
人工智能 数据可视化 编译器
Go interface实现分析
本文深入探讨了Go语言中接口的定义、实现及性能影响。接口作为一种“约定”,包含方法签名集合,无需依赖具体类型即可调用方法,隐藏了内部实现细节。文章分析了接口的两种实现方式(iface和eface)、按值与按指针实现的区别,以及nil接口与普通nil的区别。同时,通过反汇编代码对比了接口动态调用与类型直接调用的性能差异,指出接口调用存在内存逃逸和无法内联的问题。最后总结了接口的优势与局限性,强调在实际开发中需根据场景合理选择是否使用接口。
316 13
Go - struct{} 实现 interface{}
Go - struct{} 实现 interface{}
177 9
|
JSON 人工智能 编译器
Go json 能否解码到一个 interface 类型的值
Go json 能否解码到一个 interface 类型的值
181 1
|
Go
Go语言学习之 interface
Go语言学习之 interface
143 0
|
Go
go的interface怎么实现的?
面试题:go的interface怎么实现的?
265 0
|
Go Android开发 Python
实证与虚无,抽象和具象,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang接口(interface)的使用EP08
看到接口这两个字,我们一定会联想到面向接口编程。说白了就是接口指定执行对象的具体行为,也就是接口表示让执行对象具体应该做什么,所以,普遍意义上讲,接口是抽象的,而实际执行行为,则是具象的。
实证与虚无,抽象和具象,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang接口(interface)的使用EP08
|
存储 Java Go
速学Go语言接口interface
速学Go语言接口interface
|
存储 缓存 Go
如何用好 Go interface
The bigger the interface, the weaker the abstraction.
221 0
Go Interface 合法验证
- 值方法集和接口匹配 - 给接口变量赋值的不管是值还是指针对象,都ok,因为都包含值方法集 - 指针方法集和接口匹配 - 只能将指针对象赋值给接口变量,因为只有指针方法集和接口匹配 - 如果将值对象赋值给接口变量,会在编译期报错(会触发接口合理性检查机制)
230 0

热门文章

最新文章