golang之interface
一、概述
接口类型是对 ”其他类型行为“ 的抽象和概况;因为接口类型不会和特定的实现细节绑定在一起;很多面向对象都有类似接口概念,但Golang语言中interface的独特之处在于它是满足隐形实现的。也就是说,我们没有必要对于给定的具体类型定义所有满足的接口类型;简单拥有一些必需的就ok了;
此时,我们对于interface还是比较迷茫,关键在于interface与其他具体类型(除interface之后都是具体类型)的不同之处在于,interface是抽象的;比如我们看到int类型,就知道int类型具有的算术操作;切片类型的索引操作等等;具体的类型还可以通过它的方法提供额外的行为操作;总之,当拿到一个具体的类型时,就知道它本身是什么和可以用它来做什么;
interface类型:抽象的类型,它不会暴露出它所代表的对象的内部值的结构和这个对象支持的基础操作的集合(而且它自己也没有,所以接口本身也没法展示),它只会展示出它自己的方法;
也就是说,当看到一个interface时,我们并不知道它是什么,唯一知道的就是可以通过它的方法来做什么;(这会又迷糊了...zZZ)
func Printf(format string, a ...interface{}) (n int, err error){
return Fprintf(os.Stdout, format, a...)
}
func Sprintf(format string, args ...interface{}) string{
var buf bytes.Buffer
Fprintf(&buf, format, args...)
return buf.String()
}
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error){
...
}
关键点在于:第2行,第7行中调用Fprintf()函数时,传递的第一个参数;
第2行实参: os.Stdout 是*os.File类型
第7行实参: buf 是bytes.Buffer类型
而实现函数的形参是io.Writer类型,是interface类型
package io
// Writer is the interface that wraps the basic Write method.
type Writer interface {
// Write writes len(p) bytes from p to the underlying data stream.
// It returns the number of bytes written from p (0 <= n <= len(p))
// and any error encountered that caused the write to stop early.
// Write must return a non-nil error if it returns n < len(p).
// Write must not modify the slice data, even temporarily.
//
// Implementations must not retain p.
Write(p []byte) (n int, err error)
}
此时就将interface的意义展示的淋漓尽致了;
interface(io.Write) 定义了函数Fprintf和该函数的调用者之间的约定:
- 对调用者的约束:实参必须有一个特定的签名和提供一个Write函数
- 对函数的约束: Fprintf接受任何满足io.Writer接口的参数都可以正常工作
一个类型(形参)可以自由的使用另一个满足相同接口的类型(实参)来进行替换被称为可替代性(LSP里氏替换) —— c++的多态
package main
import "fmt"
type Msg struct {
Id int
Name string
}
func (this *Msg) Write(p []byte) (int, error) {
this.Id += int(len(p))
this.Name = "interface"
return this.Id, nil
}
func main() {
msg := new(Msg)
msg.Id =
msg.Name = "interface test print"
var name = "Dolly"
fmt.Fprintf(msg, "%s", name) //convert msg, call Write(my func)
fmt.Println(msg) //stdout
}
调用fmt.Fprintf函数,第一个参数传入自定义的参数,该参数必须实现Write函数,否则编译报错;
二、接口类型
接口类型具体描述了一系列方法的集合,一个实现了这些方法的具体类型称为该接口的实例;
golang之interface的更多相关文章
- Golang的Interface是个什么鬼
问题概述 Golang的interface,和别的语言是不同的.它不需要显式的implements,只要某个struct实现了interface里的所有函数,编译器会自动认为它实现了这个interfa ...
- golang 关于 interface 的学习整理
Golang-interface(四 反射) go语言学习-reflect反射理解和简单使用 为什么在Go语言中要慎用interface{} golang将interface{}转换为struct g ...
- golang的interface剖析
背景: golang的interface是一种satisfied式的.A类只要实现了IA interface定义的方法,A就satisfied了接口IA.更抽象一层,如果某些设计上需要一些更抽象的 ...
- Golang 的 `[]interface{}` 类型
Golang 的 []interface{} 类型 我其实不太喜欢使用 Go 语言的 interface{} 类型,一般情况下我宁愿多写几个函数:XxxInt, XxxFloat, XxxString ...
- Golang接口(interface)三个特性(译文)
The Laws of Reflection 原文地址 第一次翻译文章,请各路人士多多指教! 类型和接口 因为映射建设在类型的基础之上,首先我们对类型进行全新的介绍. go是一个静态性语言,每个变量都 ...
- Golang-interface(四 反射)
github:https://github.com/ZhangzheBJUT/blog/blob/master/reflect.md 一 反射的规则 反射是程序执行时检查其所拥有的结构.尤其是类型的一 ...
- golang将interface{}转换为struct
项目中需要用到golang的队列,container/list,需要放入的元素是struct,但是因为golang中list的设计,从list中取出时的类型为interface{},所以需要想办法把i ...
- Golang-interface(二 接口与nil)
github: https://github.com/ZhangzheBJUT/blog/blob/master/nil.md 一 接口与nil 前面解说了go语言中接口的基本用法,以下将说一说nil ...
- golang(10)interface应用和复习
原文链接 http://www.limerence2017.com/2019/10/11/golang15/ interface 意义? golang 为什么要创造interface这种机制呢?我个人 ...
随机推荐
- VF(动态规划)
VF 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Vasya is the beginning mathematician. He decided to make a ...
- MySQL搜索: WHERE 多条件
WHERE能够按多条件进行搜索. products表例如以下: a WHERE后以and 接两个条件以及三个条件进行搜索: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...
- Python标准库:内置函数classmethod(function)
把类函数当作类的一个方法返回. 类方法第一个參数是指明类,跟类中函数一样,第一个參数是指明类实例. 类方法修饰符採用以下的格式来使用: class C: @classmethod def f(cls, ...
- jquery工具
http://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxsplitter/jquery-splitter-gett ...
- bootstrap之Flick
Flick package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiDevice; imp ...
- C# Socket学习笔记二
小记:昨天咱们已经了解了Socket的通信原理,可是点对点的一次通信并不是我们想要的,那么今天那我们就继续学习异步通信,简单来说就是服务器端和客户端可以进行多次 互发信息的通信而不用担心通道会关闭.在 ...
- jfreechart中文乱码问题解决方案(转)
参考网址:http://zhidao.baidu.com/link?url=y88rR1_aAHaFofonx9o_IaEu87MpkTQImsqDcy587eG55JkfQV6EzzzloIgXuQ ...
- SQL子句执行顺序和Join的一点总结
SQL子句执行顺序和Join的一点总结 FROM ON JOIN WHERE GROUP BY WITH CUBE or WITH ROLLUP HAVING SELECT DISTINCT ORDE ...
- linux下C++ STL hash_map的使用以及使用char *型变量作为Key值的一大“坑”
计算机编程中经常会用到hash表,而在C++中,使用STL编程更是少不了的.本文将介绍STL中hash_map的使用.在hash_map中使用自定义类型作为key值的方法以及在使用char *类型作为 ...
- ng-click得到当前元素,angular.element()用法
<!DOCTYPE html> <html> <head> <title></title> <script src="lib ...