Go中没有class的概念。Go 语言中使用组合实现对象特性的描述。对象的内部使用结构体内嵌组合对象应该具有的特性,对外通过接口暴露能使用的特性。
Go 语言的接口设计是非侵入式的,接口不知道接口被哪些类型实现。而实现不用指明具体实现哪一个接口。编译时时编译器会指明使用哪个类型实现哪个接口。

只有让接口和实现解耦,编译速度才能真正提高,项目之间的耦合度也会降低不少。
很多其他语言实现接口时,是必须指定接口的。

实现接口

只有接口的方法名,参数,返回值都在某一类型中对应上,我们才说这个类型实现了接口。

 package main

 import (
"fmt"
) // interface
type Animal interface {
Cry(data interface{}) error
} // stuct
type Cat struct {
} func (cat *Cat) Cry(data interface{}) error {
fmt.Println("Mi:", data)
return nil
}
func main() {
cat := new(Cat)
var animal Animal
animal = cat
animal.Cry("Ao")
}

在16行Cat实现了Animal的唯一方法,所以在23,14行我们可以使用animal调用cat的方法。

sort.Interface


sort.interface是Go内置的一个帮助排序的接口。接口的定义如下

 // A type, typically a collection, that satisfies sort.Interface can be
// sorted by the routines in this package. The methods require that the
// elements of the collection be enumerated by an integer index.
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}

其中包含排序需要的:数量(Len)、比较(Less)、交换(Swap)

实现接口

 package main

 import (
"fmt"
"sort"
) type IntList []int func (i IntList) Len() int {
return len(i)
}
func (i IntList) Less(a, b int) bool {
return i[a]% < i[b]%
}
func (i IntList) Swap(a, b int) {
i[a], i[b] = i[b], i[a]
}
func main() {
ints := IntList{, , , , , , , }
sort.Sort(ints)
for _, v := range ints {
fmt.Printf("%s\n", v)
}
}

这个代码并不复杂,主要看清除4行即可。打印结果

%!s(int=)
%!s(int=)
%!s(int=)
%!s(int=)
%!s(int=)
%!s(int=)
%!s(int=)
%!s(int=)

使用sort.Slice进行切片元素排序

sort.Slice() 函数进行更为简便的排序方法,把上面的代码稍微整理之后:

 package main

 import (
"fmt"
"sort"
) type IntList []int func main() {
ints := IntList{, , , , , , , }
sort.Slice(ints, func(i, j int) bool { return ints[i]% < ints[j]%
})
for _, v := range ints {
fmt.Printf("%s\n", v)
}
}

Golang:接口(interface)的更多相关文章

  1. Golang 接口interface

    接口interface 接口是一个或多个方法签名的集合 只要某个类型拥有该接口的所有方法签名,即算实现该接口,无需显示声明实现了哪个接口,这成为Structural Typing 接口只有方法声明,没 ...

  2. Golang接口(interface)三个特性(译文)

    The Laws of Reflection 原文地址 第一次翻译文章,请各路人士多多指教! 类型和接口 因为映射建设在类型的基础之上,首先我们对类型进行全新的介绍. go是一个静态性语言,每个变量都 ...

  3. 【Golang】Go 通过结构(struct) 实现接口(interface)

    一.通过结构(struct) 实现 接口(interface) 1.在了解iris框架的时候,经常看到有这样去写的使用一个空结构体作为接收器,来调用方法,有点好奇这样做有什么意义. 解释:在 Go 语 ...

  4. Golang的Interface是个什么鬼

    问题概述 Golang的interface,和别的语言是不同的.它不需要显式的implements,只要某个struct实现了interface里的所有函数,编译器会自动认为它实现了这个interfa ...

  5. Go语言学习笔记(四)结构体struct & 接口Interface & 反射

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 结构体struct struct 用来自定义复杂数据结构,可以包含多个字段(属性),可以嵌套: go中的struc ...

  6. Go 接口(interface)

        文章转载地址:https://www.flysnow.org/2017/04/03/go-in-action-go-interface.html 1.什么是 interface? 简单的说,i ...

  7. golang的interface剖析

      背景: golang的interface是一种satisfied式的.A类只要实现了IA interface定义的方法,A就satisfied了接口IA.更抽象一层,如果某些设计上需要一些更抽象的 ...

  8. Go语言学习笔记(四)结构体struct & 接口Interface & 反射reflect

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 结构体struct struct 用来自定义复杂数据结构,可以包含多个字段(属性),可以嵌套: go中的struc ...

  9. 【Golang 接口自动化06】微信支付md5签名计算及其优化

    前言 可能看过我博客的朋友知道我主要是做的支付这一块的测试工作.而我们都知道现在比较流行的支付方式就是微信支付和支付宝支付,当然最近在使用低手续费大力推广的京东金融(已改名为京东数科)以后也可能站到第 ...

  10. Golang 接口与反射知识要点

    目录 Golang 接口与反射知识要点 1. 接口类型变量 2. 类型断言 3. 鸭子类型 4. 反射机制 5. reflect 包 TypeOf().ValueOf() Type().Kind() ...

随机推荐

  1. sql脚本过大,无法打开的解决方法

    打开cmd命令窗口,输入如下命令: sqlcmd -S ipaddress -U user -P password -d dbname -i file 其中,ipaddress是数据库服务器ip,us ...

  2. eShopOnContainers 看微服务⑤:消息通信

    1.消息通信 传统的单体应用,组件间的调用都是使用代码级的方法函数.比如用户登录自动签到,增加积分.我们可以在登录函数调用积分模块的某个函数,为了解耦我们使用以来注入并放弃new Class()这种方 ...

  3. IntelliJ IDEA插件系列

    参考: IntelliJ IDEA插件系列 1. activate-power-mode 和 Power mode II 根据Atom的插件activate-power-mode的效果移植到IDEA上 ...

  4. swing 嵌入浏览器

    需求要在swing加一个浏览器,在网上找了一个挺方便的方法,现在把代码贴上来 力求方便. package com.vtradex.page.shipment; import static javafx ...

  5. 异常:Instantiation of bean failed; nested exception is java.lang.NoSuchMethodError: com.google.common.base.Preconditions.che ckState(ZLjava/lang/String;I)V

    Instantiation of bean failed; nested exception is java.lang.NoSuchMethodError: com.google.common.bas ...

  6. java8 for ,forEach ,lambda forEach , strean forEach , parller stream forEach, Iterator性能对比

    java8 for ,forEach ,Iterator,lambda forEach ,lambda  strean forEach , lambda parller stream forEach性 ...

  7. react源码第一天

    1.下载源码:github 16.7版本 2.找到笔记:https://react.jokcy.me/book/api/react.html#

  8. DRF框架之认证组件用法(第四天)

    1. 什么是drf 框架的认证组件: auth 就等于是jango中的Auth模块,Auth是自带session信息,但是 drf的认证组件可以自定义token携带过去,去判断用的 2.如何实现认证呢 ...

  9. Spring中利用applicationContext.xml文件实例化对象和调用方法

    Spring中实例化对象和调用方法入门 1.jar包和xml的准备 已上传至百度云盘,链接: https://pan.baidu.com/s/1CY0xQq3GLK06iX7tVLnp3Q 提取码: ...

  10. html和css问题?

    1.说说你对语义化的理解?答,去掉或者丢失样式的时候能够让页面呈现出清晰的结构方便其他设备解析(如屏幕阅读器.盲人阅读器.移动设备)以意义的方式来渲染网页:便于团队开发和维护,语义化更具可读性,是下一 ...