Go language types and interfaces

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the go language, as long as a type implements an interface containing a method, we can assume that the type implements the interface, the following is an example of a simple type and interface in the go language.

Package Mainimport (    "FMT")//define an interface to implement the Run method type Irun interface {    run ()}//define an adult type of man struct {}// Define a child type a kid struct {}//implements the Run method for adults, and essentially implements the Irun interface func (M *man) run () {FMT) for adults    . PRINTLN ("Adult Running")}//for the child to implement the Run method, the essence is to implement the child Irun interface func (k *kid) run () {    fmt. PRINTLN ("Child Running")}func main () {    var krun irun = new (Kid)    Krun.run ()    var mrun irun = new (man)    Mrun.run ()}

X---------------------running Results------------------x//

The kid's running.

The adults are running.


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.