温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具
所有文章 友链 关于我

温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具

Go 简单了解 interface

2019-02-19

在 Java 中 interface 是很常用的,因为父类单继承的特性,interface 可以让类的实现更加灵活。

而同样在强类型的 GO 中,interface 也是必不可少的。



  • interface 的实现
  • 判断 interface 存储值的类型
  • 使用 interface 实现泛型

interface 的实现

1
2
3
type I interface {

}

interface 是一种类型,可以包含 0 或更多的方法,如果一个类型实现了一个 interface 的所有方法,则它实现了该 interface,比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
// interface 的实现

import (
"fmt"
)

type IUser interface {
SetName(string)
GetName() string
}

type User struct {
name string
}


func (self User) GetName() string {
return self.name
}

func (self *User) SetName(name string) {
self.name = name
}

func main() {

var i IUser
i = &User{}
i.SetName("wxnacy")
fmt.Println(i.GetName())
// wxnacy

}

可以看到,因为 SetName 方法是在指针中实现的,所有 i 需要储存指针才可以实现 struct 中的所有方法

判断 interface 存储值的类型

使用 i.(T) 或 i.(type) 可以判断 interface 中储存的值,后者只能在 switch 中使用。

i 是 interface 类型的变量,T 代表要断言的类型,value 是 interface 变量存储的值,ok 是 bool 类型表示是否为该断言的类型 T

1
2
3
4
if t, ok := i.(*User); ok {
fmt.Println(t)
}
// &{User}
1
2
3
4
5
6
7
8
9
10
switch t := m.(type) {
case Man: {
fmt.Println("m is ", t)
}
default: {
fmt.Println("m is default")
}
}

// m is {Man}

使用 interface 实现泛型

强类型语言可以实现类似 Java 的泛型是必不可少的,在 Go 中可以使用 interface 来实现。

1
2
3
func InArray(val interface{}, array interface{}) (index int){

}

避免通过编写重复代码实现方法的多态,我们可以使用 interface{} 来传入任意类型,
并使用 reflect 反射包来实现具体方法。

我们使用该特性,实现一个判断数组中是否包含某值的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 利用 reflect 反射方法判断 val 是否包含在 array 中,如果包含返回索引位置
func InArray(val interface{}, array interface{}) (index int) {
index = -1
switch reflect.TypeOf(array).Kind() {
case reflect.Slice: {
arr := reflect.ValueOf(array)
for i := 0; i < arr.Len(); i++ {
if reflect.DeepEqual(val, arr.Index(i).Interface()) {
index = i
return
}
}
}
}
return
}
var arr = []int{1,2,3,4}
fmt.Println(InArray(3, arr))
// 2
  • reflact

Demo 地址:https://github.com/wxnacy/study/tree/master/goland/src/interfaces

最近更新
Alfred Workflow 命令行帮助工具
最近热读
Go 判断数组中是否包含某个 item
Vim 高级功能 vimgrep 全局搜索文件
办理北京工作居住证的一些细节
Go 语法错误:Non-declaration statement outside function body
Mac 电脑查看字体文件位置
扫码关注公众号,或搜索公众号“温欣爸比” 及时获取我的最新文章
赏

谢谢你请我喝咖啡

支付宝
微信
  • go
简单理解时间复杂度
Python 单例的四种实现方式
  1. 1. interface 的实现
  2. 2. 判断 interface 存储值的类型
  3. 3. 使用 interface 实现泛型
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量3620次 访客数3571人次 本文总阅读量2次
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • python
  • flask
  • javascript
  • docker
  • 工具
  • openresty
  • 微信
  • java
  • hexo
  • 杂谈
  • vim
  • git
  • mysql
  • http
  • linux
  • mac
  • tmux
  • ssh
  • 算法
  • 开发
  • node
  • 杂文
  • jinja2
  • maven
  • spring
  • 北京
  • 生活
  • springboot
  • react
  • shell
  • graphql
  • iterm
  • expect
  • nginx
  • sqlalchemy
  • html
  • electron
  • vagrant
  • elastic
  • 宝贝
  • ansible
  • css
  • jquery
  • go
  • markdown
  • awk
  • redis
  • leetcode
  • zsh
  • 漫威
  • ssr
  • android
  • ffmpeg
  • chrome
  • vmware
  • youtube
  • windows
  • jupyter
  • excel
  • jq
  • Mac
  • Homebrew
  • mongo
  • py2
  • HomeBrew
  • movie
  • nodejs

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Guru99
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆