接口

接口原理

在 Golang 中,interface 是一种抽象类型,相对于抽象类型的是具体类型(concrete type):int,string。

在 Golang 中,interface 是一组 method 的集合,是 duck-type programming 的一种体现。不关心属性(数据),只关心行为(方法)。具体使用中你可以自定义自己的 struct,并提供特定的 interface 里面的 method 就可以把它当成 interface 来使用。下面是一种 interface 的典型用法,定义函数的时候参数定义成 interface,调用函数的时候就可以做到非常的灵活。

type MyInterface interface{ 
         Print()
}
func TestFunc(x MyInterface) {
}
type MyStruct struct{
}
func (me *MyStruct) Print() {
}
func main() {
var me Mystruct 
TestFunc(me)
} 
  • 优点
  • writing generic algorithm (泛型编程)
  • 我们现在要写一个泛型算法,形参定义采用 interface 就可以了。在具体调用的时候传入具体类型,并且具体类型实现了sort接口的方法。如上文实现

  • hiding implementation detail (隐藏具体实现)
  • 隐藏具体实现,这个很好理解。比如我设计一个函数给你返回一个 interface,那么你只能通过 interface 里面的方法来做一些操作,但是内部的具体实现是完全不知道的

  • providing interception points (提供切入时机 非侵入式)

反射reflect

    在 reflect 包中有两个类型须要了解:Type 和 Value。这两个类型使得能够訪问接口变量的内容。还有两个简单的函数,reflect.TypeOf 和 reflect.ValueOf,从接口值中分别获取 reflect.Type 和 reflect.Value。

(注:从 reflect.Value 也非常easy可以获得 reflect.Type,只是这里让 Value 和 Type 在概念上是分离的)

package main

import (
     "fmt"
    "reflect"
)

func main() {
    var x float64 = 3.4
    fmt.Println("type:", reflect.TypeOf(x))
}
接口类型判断 v.Kind() == reflect.Float64
var x float64 = 3.4
v := reflect.ValueOf(x)
fmt.Println("type:", v.Type())
fmt.Println("kind is float64:", v.Kind() == reflect.Float64)
fmt.Println("value:", v.Float()) 
实例:
//完成以下函数,实现比较两个任意整型或浮点类型的变量大小的功能,
//异常情况:
//1.不支持非整型,非浮点型数据的比较, 异常请返回error: "type not supported"
//2.l,r类型不相同请返回error: "type not match"
func Less(l, r interface{}) (error, bool) {

    if !strings.Contains(reflect.TypeOf(l).String(), "int")&&!strings.Contains(reflect.TypeOf(l).String(), "float"){
        var val error = errors.New("type not supported")
        return  val,false
    }
    if !strings.Contains(reflect.TypeOf(r).String(), "int")&& !strings.Contains(reflect.TypeOf(r).String(), "float"){
        var val1 error = errors.New("type not supported")
        return  val1,false
    }
    if reflect.TypeOf(l).String()!= reflect.TypeOf(r).String(){
        var val2 error = errors.New("type not match")
        return  val2,false
    }else {
        if strings.Contains(reflect.TypeOf(l).String(), "int") {
            if reflect.TypeOf(l).String()=="int"{
               if l.(int) - r.(int) < 0 {
                   return nil, true
               } else {
                   return  nil,false
               }
            }else if reflect.TypeOf(l).String()=="uint"{
                if l.(uint) < r.(uint)  {
                    return nil, true
                } else {
                    return  nil,false
                }
            }

        } else  {
            if l.(float64) - r.(float64)< 0 {
                return nil, true
            } else {
                return nil,false
            }
        }
    }
    return nil,false
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值