golang中接口interface和struct结构类的分析

再golang中,我们要充分理解interface和struct这两种数据类型。为此,我们需要优先理解type的作用。

type是golang语言中定义数据类型的唯一关键字。对于type中的匿名成员和指针成员,这里先不讲,重点讲解interface和struct这两种特殊的数据类型。

interface和struct也是数据类型,特殊在于interface作为万能的接口类型,而struct作为常用的自定义数据类型的关键字。说到这里相比大家已经明白interface的侧重点在于接口的定义(方法),而struct侧重点在于数据结构的定义。使用struct定义了数据结构,可以直接使用func方法定义数据结构中使用的方法。但是为了解耦,为了扩展,一般在真正设置功能性函数时,除了内置的数据类型外,都推荐使用接口的方法来传递相关方法。

既然推荐使用接口的方法来作为参数,那么具体结构中的数据成员又如何访问呢?golang提供了非常方便的类型查询和类型转换方法。名称.(type)用于类型查询,名称.(具体类型)用于类型转换。因此,使用接口作为函数参数是很有价值的。

另外,我们还需要明白对象和对象指针在golang中的微妙处理,golang可以自动将对数据类型定义的方法抓转换成对数据类型指针定义的方法。但不能自动的将对数据类型指针的方法转换为对数据类型定义的方法。

以下是我们的测试程序,注释已经注明了相关说明。

package main

import "fmt"

//定义一个接口
type Stryc interface {
    Int() int
}

//定义一个自定义数据类型
type myCls struct {
    value int
}
//使用数据类型指针绑定方法(在调用方法时,数据类型在golang中会默认转换为数据类型指针进行使用)
func(a * myCls) Int() int{
    return a.value
}
//定义一个函数,参数为一个自定义的数据类型
func printTheValueByStruct(arg myCls){
    fmt.Printf("the value is %d \n",arg.Int())
}
//定义一个函数,参数为一个接口
func printTheValue(arg Stryc) int{
    fmt.Printf("the value is %d \n",arg.Int()/*使用接口调用接口的方法*/)
    return arg.Int()
}

//定义一个函数,参数为动态个数的接口类型参数
func printAnyValue(args ...interface{}){
    //使用for range方法获取每一个接口
    for _,arg := range args{
        //使用.(type)方法查询接口的数据类型
        switch arg.(type) {
        case int:
            fmt.Println("the type is int")
        case string:
            fmt.Println("the type is string")
        case myCls:/*是自定义数据类型*/
            //使用.(数据类型)进行强转
            var b myCls = arg.(myCls)
            fmt.Println("the type is myCls, the function value is ", b.Int()/*d调用数据类型的方法,golang会转换为数据指针类型调用*/,"the struct value is ", b.value/*调用数据结构的数据*/)
        case Stryc:/*是定义的接口数据类型*/
            fmt.Println("the type is Stryc interface, the function value is ", arg.(Stryc).Int()/*将接口强转到指定接口,并调用方法*/)
        }
    }
}

func main(){
    var    V1 *myCls = new(myCls) //创建一个对象指针
    V1.value = 1111111 //给对象赋值
    var V2 myCls = myCls{222222222} //创建一个对象,给对象赋值
    var a interface {}=myCls{33333} //创建一个对象,将对象赋值后传给一个万能类型接口
    var a1 interface{}=&myCls{444444}//创建一个对象,将对象指针传给一个万能类型接口
    fmt.Println("hello world!")

    printTheValue(V1) //V1会转换为Stryc接口被调用其中的方法
    printTheValue(a1.(Stryc)) //万能接口a1中放置的对象指针被强制转为Stryc接口调用
    printTheValueByStruct(*V1) //强制将V1的对象使用*显示传入函数,因为参数是对象
    printTheValueByStruct(a.(myCls))//强制将万能接口a中放置的对象转换为对象传入函数,因为参数是对象

    printTheValue(&V2) //将对象的指针传入函数,golang将其转换为Stryc接口
    printAnyValue(V1/*传入一个指针,会同Stryc接口数据类型匹配*/,
                  V2/*传入一个对象,会同myCls数据类型匹配*/,
                  *V1/*将指针显示为对象传入,会同myCls数据类型匹配*/,
                  &V2/*将对象的指针传入,会同Stryc接口数据类型匹配*/)
}

以下是执行结果

hello world!
the value is 1111111 
the value is 444444 
the value is 1111111 
the value is 33333 
the value is 222222222 
the type is Stryc interface, the function value is  1111111
the type is myCls, the function value is  222222222 the struct value is  222222222
the type is myCls, the function value is  1111111 the struct value is  1111111
the type is Stryc interface, the function value is  222222222

 

posted @ 2019-03-27 23:42  在左手  阅读(8674)  评论(0编辑  收藏  举报