Golang struct Implementation Interface

Source: Internet
Author: User

Golang, Strcut contains interface types, the struct type needs to implement interface exported interfaces, thus becoming the corresponding interface interface classes.

In fact, after a struct contains interface, it does not need to implement the interface interface, or it can become a interface interface class.

The code is as follows:

Type newer interface {    

    new ()

}

T Ype TestInterface Interface {    

    newEr    

    done () <-chan struct{}

}

Type kktest struct {    

    testinterface

}

Func newtest () newer {    

& Nbsp;   return kktest{}

}

Func main () {    

   &NBSP;&NBSP;KK: = Newtest ()     

    i,ok: = Kk. ( TestInterface)     

    fmt. Println (I,ok)     

    ch: = I.done ()     

    fmt. PRINTLN (CH)

}

where I,ok: = Kk. ( TestInterface) test success, that is, Kktest is already the TestInterface interface class, but the subsequent ch: = I.done () raises panic, this is also expected.

Related applications can look at the implementation of the context package, VALUECTX partial implementation of the context interface function, to its unwanted functions are not implemented, if the call to these non-implemented functions will cause panic. This is a good thing to do in the wrong way, because calling to these interfaces means that the code is actually written incorrectly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.