• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Golang > 初步解读Golang中的接口相关编写方法

初步解读Golang中的接口相关编写方法

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了golang 接口,golang 接口指针,golang 空接口,golang 方法,golang init方法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

概述
如果说goroutine和channel是Go并发的两大基石,那么接口是Go语言编程中数据类型的关键。在Go语言的实际编程中,几乎所有的数据结构都围绕接口展开,接口是Go语言中所有数据结构的核心。
Go语言中的接口是一些方法的集合(method set),它指定了对象的行为:如果它(任何数据类型)可以做这些事情,那么它就可以在这里使用。

接口的定义和使用

比如

type I interface{
    Get() int
    Put(int)
 
}

</div>
这段话就定义了一个接口,它包含两个函数Get和Put

好了,我的一个接口实现了这个接口:

type S struct {val int}
func (this *S) Get int {
    return this.val
}
func (this *S)Put(v int) {
    this.val = v
 
}

</div>
这个结构S就是实现了接口I

Go中interface的写法

下面看几个interface的例子:

func SomeFunction(w interface{Write(string)}){
    w.Write("pizza")
 
}

</div>
这个例子中,直接将interface定义在参数中,很特别…

func weirdFunc( i int ) interface{} {
  if i ==  0 {
    return "zero"
  }
  return i;
}

</div>
接口赋值
我们可以将一个实现接口的对象实例赋值给接口,也可以将另外一个接口赋值给接口。

(1)通过对象实例赋值

将一个对象实例赋值给一个接口之前,要保证该对象实现了接口的所有方法。考虑如下示例:

type Integer int
func (a Integer) Less(b Integer) bool {
 return a < b
}
func (a *Integer) Add(b Integer) {
 *a += b
}

type LessAdder interface {
 Less(b Integer) bool
 Add(b Integer)
}

var a Integer = 1
var b1 LessAdder = &a //OK
var b2 LessAdder = a   //not OK

</div>
b2的赋值会报编译错误,为什么呢?还记得<类型方法>一章中讨论的Go语言规范的规定吗?

The method set of any other named type T consists of all methods with receiver type T. The method set of the corresponding pointer type T is the set of all methods with receiver T or T (that is, it also contains the method set of T).
也就是说*Integer实现了接口LessAdder的所有方法,而Integer只实现了Less方法,所以不能赋值。

(2)通过接口赋值

        var r io.Reader = new(os.File)
        var rw io.ReadWriter = r   //not ok

        var rw2 io.ReadWriter = new(os.File)
        var r2 io.Reader = rw2    //ok

</div>
因为r没有Write方法,所以不能赋值给rw。

接口嵌套
我们来看看io package中的另外一个接口:

// ReadWriter is the interface that groups the basic Read and Write methods.
type ReadWriter interface {
 Reader
 Writer
}

</div>
该接口嵌套了io.Reader和io.Writer两个接口,实际上,它等同于下面的写法:

type ReadWriter interface {
Read(p []byte) (n int, err error)
Write(p []byte) (n int, err error)
}

</div>
注意,Go语言中的接口不能递归嵌套,

// illegal: Bad cannot embed itself
type Bad interface {
 Bad
}

// illegal: Bad1 cannot embed itself using Bad2
type Bad1 interface {
 Bad2
}
type Bad2 interface {
 Bad1
}

</div>

空接口(empty interface)
空接口比较特殊,它不包含任何方法:

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

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

</div>
如果函数打算接收任何数据类型,则可以将参考声明为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)

</div>
注意,[]T不能直接赋值给[]interface{}

        t := []int{1, 2, 3, 4}
        var s []interface{} = t

</div>
编译时会输出下面的错误:

cannot use t (type []int) as type []interface {} in assignment

我们必须通过下面这种方式:

您可能想查找下面的文章:

  • golang使用sort接口实现排序示例
  • 初步解读Golang中的接口相关编写方法
  • Golang极简入门教程(二):方法和接口

相关文章

  • Go语言写入字符串到文件的方法
  • Go语言中关闭带缓冲区的频道实例分析
  • Golang极简入门教程(二):方法和接口
  • Go语言中接口组合的实现方法
  • Golang中的sync.WaitGroup用法实例
  • 举例讲解Go语言中函数的闭包使用
  • go语言中if语句用法实例
  • Go语言中struct的匿名属性特征实例分析
  • Go语言中的复合类型详细介绍
  • 我为什么喜欢Go语言(简洁的Go语言)

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Go语言map用法实例分析
    • Go语言实现socket实例
    • Go语言实现类似c++中的多态功能实例
    • go语言的sql包原理与用法分析
    • Go语言执行系统命令行命令的方法
    • go语言接口用法实例分析
    • Go语言中append函数用法分析
    • Go语言Cookie用法分析
    • Go语言使用MySql的方法
    • golang使用正则表达式解析网页

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有