Go语言基础go接口用法示例详解

作者:枫少文 时间:2024-04-30 10:06:53 

目录
  • 概述

  • 语法

    • 定义接口

    • 实现接口

    • 空接口

    • 接口的组合

  • 总结

    Go语言基础go接口用法示例详解

    概述

    Go 语言中的接口就是方法签名的集合,接口只有声明,没有实现,不包含变量。

    语法

    定义接口


    type [接口名] interface {
       方法名1(参数列表) 返回值列表  
       方法名2(参数列表) 返回值列表
       ...
    }

    例子


    type Isay interface{
     sayHi()
    }

    实现接口

    例子


    //定义接口的实现类
    type Chinese struct{}
    //实现接口
    func (_ *Chinese) sayHi() {
     fmt.Println("中国人说嗨")
    }

    //中国人
    type Chinese struct{}
    //美国人
    type Americans struct{}
    func (this *Chinese) sayHi()  {
     fmt.Println("中国人说嗨")
    }
    func (this Americans) sayHi()  {
     fmt.Println("美国人说hi")
    }
    //调用
    &Chinese{}.sayHi()
    Americans{}.sayHi()

    空接口

    在Go语言中,所有其它数据类型都实现了空接口。


    interface{}

    var v1 interface{} = 1
    var v2 interface{} = "abc"
    var v3 interface{} = struct{ X int }{1}

    如果函数打算接收任何数据类型,则可以将参考声明为interface{}。最典型的例子就是标准库fmt包中的Print和Fprint系列的函数:


    func Fprint(w io.Writer, a ...interface{}) (n int, err error)
    func Fprintf(w io.Writer, format string, a ...interface{})
    func Fprintln(w io.Writer, a ...interface{})
    func Print(a ...interface{}) (n int, err error)
    func Printf(format string, a ...interface{})
    func Println(a ...interface{}) (n int, err error)

    接口的组合

    一个接口中包含一个或多个接口


    //说话
    type Isay interface{
     sayHi()
    }
    //工作
    type Iwork interface{
     work()
    }

    //定义一个接口,组合了上述两个接口
    type IPersion interface{
     Isay
     Iwork
    }

    type Chinese struct{}

    func (_ Chinese) sayHi() {
     fmt.Println("中国人说中国话")
    }

    func (_ Chinese) work() {
    fmt.Println("中国人在田里工作")
    }

    //上述接口等价于:
    type IPersion2 interface {
    sayHi()
    work()
    }

    package main
    import "fmt"
    //中国话
    type Isay interface {
    sayHi()
    }
    //工作
    type Iwork interface {
    work()
    }
    //中国人
    type Chinese struct{}
    //美国人
    type Americans struct{}
    func (this *Chinese) sayHi() {
    fmt.Println("中国人说嗨")
    }
    func (this Americans) sayHi() {
    fmt.Println("美国人说hi")
    }
    type IPersion interface {
    Isay
    Iwork
    }
    func (_ Chinese) work() {
    fmt.Println("中国人在田里工作")
    }
    func main() {
    var chinese Isay = &Chinese{}
    chinese.sayHi()
    Americans{}.sayHi()
    //接口组合
    var ipersion IPersion = &Chinese{}
    ipersion.sayHi()
    ipersion.work()
    }

    来源:https://blog.csdn.net/guofeng93/article/details/92675978

    标签:Go,基础,接口
    0
    投稿

    猜你喜欢

  • 使用pandas忽略行列索引,纵向拼接多个dataframe

    2022-05-23 08:52:42
  • python绘制双Y轴折线图以及单Y轴双变量柱状图的实例

    2023-06-02 00:29:11
  • Python 'takes exactly 1 argument (2 given)' Python error

    2022-04-19 00:26:05
  • python爬取51job中hr的邮箱

    2022-11-06 14:00:54
  • 基于TensorBoard中graph模块图结构分析

    2021-01-11 16:58:52
  • Python统计列表元素出现次数的方法示例

    2021-03-06 00:43:23
  • 终结IE6下背景图片闪烁问题

    2009-03-04 10:11:00
  • fckeditor 修改记录添加行距功能插件

    2022-04-20 23:05:23
  • vue实现百度搜索下拉提示功能实例

    2024-04-28 09:31:15
  • MySQL删除外键、增加外键以及删除主键、增加主键的实战步骤

    2024-01-26 00:58:52
  • Python实现subprocess执行外部命令

    2021-10-04 13:42:27
  • tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用

    2022-06-29 16:23:40
  • python 实现视频流下载保存MP4的方法

    2021-09-13 06:31:38
  • python pandas 时间日期的处理实现

    2021-09-05 02:38:07
  • python实现将中文日期转换为数字日期

    2023-07-10 10:39:33
  • python如何更新包

    2023-12-06 10:36:59
  • JavaScript实现图片放大预览效果

    2023-08-23 02:41:17
  • python中urllib模块用法实例详解

    2022-02-05 13:23:33
  • MySQL性能参数详解之Max_connect_errors 使用介绍

    2024-01-21 13:31:36
  • 不用mod_rewrite直接用php实现伪静态化页面代码

    2023-11-01 07:07:45
  • asp之家 网络编程 m.aspxhome.com