美文网首页
10. Go极简教程 Interface接口

10. Go极简教程 Interface接口

作者: 超级大柱子 | 来源:发表于2018-06-03 01:30 被阅读71次

接口 interface

接口声明了方法集合

如果某个结构体实现了接口内的所有方法, 就可以把该结构体赋值给接口

接口可以帮助我们实现类似面向对象的"类的继承"

package main

import (
    "log"
)

// Abser 接口定义了方法集合
type Abser interface {
    Abs() float64
}

// Vertex -
type Vertex struct {
    X, Y float64
}

// Abs -
func (v *Vertex) Abs() float64 {
    return v.X*v.X + v.Y*v.Y
}

func main() {
    // 创建一个 Abser 接口变量 a
    var a Abser

    // 由于 *Vertex 实现了Abs(), 所以他实现了接口a的所有方法, 可以赋值给接口a
    a = &Vertex{3, 5} // 正确, *Vertex 上实现了Abs()

    // b = Vertex{3, 4}  // 运行错误, Vertex 上没有实现Abs()

    log.Println(a.Abs()) // 34
}

隐式接口

相当于合并了 其他接口的定义

package main

import (
    "log"
    "os"
)

// Reader 定义一个接口
type Reader interface {
    Read(b []byte) (n int, err error)
}

// Writer 定义一个接口
type Writer interface {
    Write(b []byte) (n int, err error)
}

// 隐式接口, 相当于合并了 其他接口的定义
type ReadWriter interface {
    Reader
    Writer
}

func main() {
    var w ReadWriter
    w = os.Stdout
    log.Println(w)      // &{0xc420098050}
    log.Println(w.Read) // 0x10b1690

}

泛型: 使用接口作为函数的参数

package main

import "log"

// Point -
type Point struct {
    x, y float64
}

// 接口作为参数
func anyParams(v interface{}) {
    // 判断 v 是否可以转换为 int 类型
    if f, ok := v.(int); ok {
        log.Println(f)
    }
    // 判断 v 是否可以转换为 string 类型
    if f, ok := v.(*Point); ok {
        log.Println(f.x + f.y)
    }
}


func main() {
    anyParams(2)                // 2
    anyParams(&Point{100, 150}) // hello T
}

参数和返回值都是接口的例子

package main

import "log"


// 接口作为参数, 接口作为返回值
func anyBack(v interface{}) interface{} {
    if f, ok := v.(int); ok {
        end := f * f
        return end
    } else if f, ok := v.(string); ok {
        end := f + f
        return end
    }
    return v
}

func main() {
    b1 := anyBack(200)
    log.Println(b1) // 40000
    b2 := anyBack("hello")
    log.Println(b2) // hellohello
}

参考资料:
http://go-tour-zh.appspot.com/
https://blog.csdn.net/hyl999/article/details/76573378

Go极简教程 继续阅读( 目录)

相关文章

  • 10. Go极简教程 Interface接口

    接口 interface 接口声明了方法集合 如果某个结构体实现了接口内的所有方法, 就可以把该结构体赋值给接口 ...

  • 01 Go极简教程 目录

    极简教程的初衷是给已有其他语言基础的人阅读尽可能少的内容学习Go语言 Go极简教程 目录 Go极简教程 环境安装及...

  • Golang interface 接口详细原理和使用技巧

    一、Go interface 介绍 interface 在 Go 中的重要性说明 interface 接口在 Go...

  • 07. Go极简教程 map的基础使用

    map的声明 map的操作 参考资料:http://go-tour-zh.appspot.com/ Go极简教程 ...

  • Go语言之Interface(一)

    Go语言之Interface(一) 什么是interface 在面向对象语言中接口是:接口定义了一个对象的行为,但...

  • Go语言之Interface(一)

    Go语言之Interface(一) 什么是interface 在面向对象语言中接口是:接口定义了一个对象的行为,但...

  • Go 语言 接口(Interface)

    What is Interface type in Go ? GoLang官网language specifica...

  • Go语言接口——interface

    Go语言的Interface接口是一种神奇的特性。Interface包括了一组方法,同时也是一种类型。Interf...

  • 十二.Go接口interface

    接口interface 接口是一个或多个方法签名的集合 只要某个类型拥有该接口的所有方法签名,即算实现该接口,无需...

  • Go语言接口interface

    Go 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现...

网友评论

      本文标题:10. Go极简教程 Interface接口

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