11 | 接口

2020-03-21  本文已影响0人  刀斧手何在

接口是用来定义行为的类型。这些被定义的行为不由接口直接实现,而是通过方法由用户自定义类型实现

v, ok := varI.(T)

如果转换合法,v 是 varI 转换到类型 T 的值,ok 会是 true;否则 v 是类型 T 的零值,ok 是 false,也没有运行时错误发生。

func TestInterface(t *testing.T){
    i := 65
    v,ok := interface{}(i).(int)
    t.Log(v,ok,i)
    //v,ok := interface{}(i).(int8)    //0,false
    //v,ok := interface{}(int8(i)).int(8)  //65,true
    //v,ok := interface{}(i).(string)    //  false
}
type file interface {
    write(msg string) bool
    read() string
}

type shell struct{
    name string
}
func (s shell) write(msg string) bool {
    fmt.Print("shell write " + msg)
    return true
}

func (s shell) read() string {
    return "hello world"
}

func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    val,ok := interface{}(sFile).(file)
    t.Log(val,ok)
    val,ok = interface{}(&sFile).(file)
    t.Log(val,ok)
}
//result: {test.sh} true
// &{test.sh} true
type file interface {
    write(msg string) bool
    read() string
}

type shell struct{
    name string
}
func (s *shell) write(msg string) bool {
    fmt.Print("shell write " + msg)
    return true
}

func (s *shell) read() string {
    return "hello world"
}

func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    val,ok := interface{}(sFile).(file)
    t.Log(val,ok)
    val,ok = interface{}(&sFile).(file)
    t.Log(val,ok)
}
//result : <nil> false
// &{test.sh} true

为什么有这个限制? 下面这句话,我还不能理解

编译器并不是总能自动获得一个值的地址

type file interface {
    writeable
    read() string
    execable
}
type writeable interface {
    write(msg string) bool
}
type execable interface{
    exec()
}

type shell struct{
    name string
}
func (s shell) write(msg string) bool {
    fmt.Print("shell write " + msg)
    return true
}
func (s shell) read() string {
    return "hello world"
}
func (s shell) exec() {
    fmt.Print("shell exec")
}
func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    val,ok := interface{}(sFile).(execable)
    //val,ok := interface{}(sFile).(writeable)
    t.Log(val,ok,reflect.TypeOf(sFile))
    val,ok = interface{}(sFile).(file)
    t.Log(val,ok,reflect.TypeOf(sFile))
}
//result : {test.sh} true test.shell
//{test.sh} true  test.shell
func TestInterface(t *testing.T){
    var any interface{}
    any = 5
    t.Log(any,reflect.TypeOf(any))
    any = "hello world"
    t.Log(any,reflect.TypeOf(any))
    any = struct{name string}{"fangle"}
    t.Log(any,reflect.TypeOf(any))
    switch tp := any.(type) {
    case int:
        t.Log(tp)
    case string:
        t.Log(tp)
    case bool:
        t.Log(tp)
    default:
        t.Log(tp)
    }
}

/*result :
5 int
hello world string
{fangle} struct { name string }
{fangle}
*/

空接口的内部实现保存了对象的类型和指针。使用空接口保存一个数据的过程会比直接用数据对应类型的变量保存稍慢。因此在开发中,应在需要的地方使用空接口,而不是在所有地方使用空接口

type file interface {
    setName(name string)
}
type shell struct{
    name string
}
func (s shell) setName(name string)  {
    s.name = name
}

func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    
    var fFace file = sFile
    //sFace.name = "hello" //报错 undifined name
    fFace.setName("hell.sh")
    t.Log(fFace,sFile,reflect.TypeOf(fFace))

    var fFace2 file = &sFile
    fFace2.setName("hello.sh")
    t.Log(fFace2,sFile,reflect.TypeOf(fFace))
}
//result : {test.sh} {test.sh} test.shell
//&{test.sh} {test.sh} test.shell

指针实现者


type file interface {
    setName(name string)
}
type shell struct{
    name string
}
func (s *shell) setName(name string)  {
    s.name = name
}

func TestInterface(t *testing.T){
    sFile := shell{"test.sh"}
    var fFace file = &sFile
    //var fFace file = sFile  //报错 接口变量 与 接口 属性
    fFace.setName("hell.sh")
    t.Log(fFace,sFile,reflect.TypeOf(fFace))
}
//result:&{hell.sh} {hell.sh} 
// *test.shell
上一篇 下一篇

猜你喜欢

热点阅读