美文网首页
11 | 接口

11 | 接口

作者: 刀斧手何在 | 来源:发表于2020-03-21 18:16 被阅读0次

接口是用来定义行为的类型。这些被定义的行为不由接口直接实现,而是通过方法由用户自定义类型实现

  • 断言
v, ok := varI.(T)

如果转换合法,v 是 varI 转换到类型 T 的值,ok 会是 true;否则 v 是类型 T 的零值,ok 是 false,也没有运行时错误发生。

func TestInterface(t *testing.T){
    i := 65
    v,ok := interface{}(i).(int)
    t.Log(v,ok,i)
    //v,ok := interface{}(i).(int8)    //0,false
    //v,ok := interface{}(int8(i)).int(8)  //65,true
    //v,ok := interface{}(i).(string)    //  false
}
  • 值接收者和指针接收者实现接口
type file interface {
    write(msg string) bool
    read() string
}

type shell struct{
    name string
}
func (s shell) write(msg string) bool {
    fmt.Print("shell write " + msg)
    return true
}

func (s shell) read() string {
    return "hello world"
}

func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    val,ok := interface{}(sFile).(file)
    t.Log(val,ok)
    val,ok = interface{}(&sFile).(file)
    t.Log(val,ok)
}
//result: {test.sh} true
// &{test.sh} true
type file interface {
    write(msg string) bool
    read() string
}

type shell struct{
    name string
}
func (s *shell) write(msg string) bool {
    fmt.Print("shell write " + msg)
    return true
}

func (s *shell) read() string {
    return "hello world"
}

func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    val,ok := interface{}(sFile).(file)
    t.Log(val,ok)
    val,ok = interface{}(&sFile).(file)
    t.Log(val,ok)
}
//result : <nil> false
// &{test.sh} true

为什么有这个限制? 下面这句话,我还不能理解

编译器并不是总能自动获得一个值的地址

  • 嵌套接口实现接口组合
type file interface {
    writeable
    read() string
    execable
}
type writeable interface {
    write(msg string) bool
}
type execable interface{
    exec()
}

type shell struct{
    name string
}
func (s shell) write(msg string) bool {
    fmt.Print("shell write " + msg)
    return true
}
func (s shell) read() string {
    return "hello world"
}
func (s shell) exec() {
    fmt.Print("shell exec")
}
func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    val,ok := interface{}(sFile).(execable)
    //val,ok := interface{}(sFile).(writeable)
    t.Log(val,ok,reflect.TypeOf(sFile))
    val,ok = interface{}(sFile).(file)
    t.Log(val,ok,reflect.TypeOf(sFile))
}
//result : {test.sh} true test.shell
//{test.sh} true  test.shell
  • 空接口interface{}
    空接口相当于 void *
func TestInterface(t *testing.T){
    var any interface{}
    any = 5
    t.Log(any,reflect.TypeOf(any))
    any = "hello world"
    t.Log(any,reflect.TypeOf(any))
    any = struct{name string}{"fangle"}
    t.Log(any,reflect.TypeOf(any))
    switch tp := any.(type) {
    case int:
        t.Log(tp)
    case string:
        t.Log(tp)
    case bool:
        t.Log(tp)
    default:
        t.Log(tp)
    }
}

/*result :
5 int
hello world string
{fangle} struct { name string }
{fangle}
*/

空接口的内部实现保存了对象的类型和指针。使用空接口保存一个数据的过程会比直接用数据对应类型的变量保存稍慢。因此在开发中,应在需要的地方使用空接口,而不是在所有地方使用空接口

  • 接口与实现者的关系
    值实现者
type file interface {
    setName(name string)
}
type shell struct{
    name string
}
func (s shell) setName(name string)  {
    s.name = name
}

func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    
    var fFace file = sFile
    //sFace.name = "hello" //报错 undifined name
    fFace.setName("hell.sh")
    t.Log(fFace,sFile,reflect.TypeOf(fFace))

    var fFace2 file = &sFile
    fFace2.setName("hello.sh")
    t.Log(fFace2,sFile,reflect.TypeOf(fFace))
}
//result : {test.sh} {test.sh} test.shell
//&{test.sh} {test.sh} test.shell

指针实现者


type file interface {
    setName(name string)
}
type shell struct{
    name string
}
func (s *shell) setName(name string)  {
    s.name = name
}

func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    var fFace file = &sFile
    //var fFace file = sFile  //报错 接口变量 与 接口 属性
    fFace.setName("hell.sh")
    t.Log(fFace,sFile,reflect.TypeOf(fFace))
}
//result:&{hell.sh} {hell.sh} 
// *test.shell

相关文章

  • 2018-06-13

    目前truesight的部署环境 truesight11接口文档地址 一 、truesight 11提供的ps接口...

  • 11 | 接口

    接口是用来定义行为的类型。这些被定义的行为不由接口直接实现,而是通过方法由用户自定义类型实现 断言 如果转换合法,...

  • 48. 服务容器 PSR-11容器接口

    Laravel 的服务容器实现了 PSR-11 接口。因此,你可以对 PSR-11容器接口类型提示来获取 Lara...

  • 关于移除和修改Bean属性的思考

    BeanFactoryPostProcessor 接口11 .所以有postProcessBeanFactory接...

  • Java - Spliterator 接口

    Java - Spliterator 接口 sschrodinger 2019/11/01 Spliterator...

  • 11.接口

    1.什么是接口 有时必须从几个父类中派生出一个子类,继承他们的方法。但是java不支持多重继承,于是接口就实现了该...

  • Spring的BeanPostProcessor执行顺序

    BeanPostProcessor的五大接口,主要的11个回调方法 1.1顶层接口BeanPostProcesso...

  • 学习pybind11(2):Hello World例子

    首先要明白pybind11是干啥的,对于一个C/C++库,可以用pybind11封装它的接口为Python接口,这...

  • WebAPI接口调试技巧

    1.了解WebAPI接口 接口模式:基于MVC4.0的WebAPI 承载协议:HTTP 跟踪工具:IE11 请求方...

  • 05 秒杀活动中关于订单系统设计到的问题

    双11对一个订单系统有多大压力? 如果每秒会发起2000个请求到订单系统的各个接口,包括下单接口、退款接口、查询接...

网友评论

      本文标题:11 | 接口

      本文链接:https://www.haomeiwen.com/subject/edbbthtx.html