Interfaces in the Golang

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

Hardware and Software Environment

    • Windows 10
    • Go 1.8

What is an interface?

Interfaces define a set of methods, but these methods do not contain the implemented code, and the interface cannot contain variables. An interface is defined as follows

type intfNamer interface {    Method1(param_list) return_type    Method2(param_list) return_type    ...}

Why design an interface?

Go is not an object-oriented programming language in the traditional sense, it does not have the concept of class and its inheritance. An interface is a contract that the implementation type must satisfy, which describes the behavior of the type, and what the specified type can do. The nature of polymorphism is that the interface completely separates what type can do and how to do it, so that variables of the same interface behave differently in different timetables.

Simple interface Example

package mainimport (    "fmt")type namer interface {    area() int}type rect struct {    width,height int}type square struct {    side int}func (r rect) area() int {    return r.height * r.width}func (s square) area() int {    return s.side * s.side}func main() {    var a = rect{4,3}    var b  = square{6}    fmt.Println(a.area())    fmt.Println(b.area())}

The result of the program execution is


Mtqq_mosquitto

Test if a value implements an interface?

type Stringer interface {    String() string}if sv, ok := v.(Stringer); ok {    fmt.Printf("v implements String(): %s\n", sv.String()) // note: sv, not v}

Interface nesting

An interface can contain one or more other interfaces, as follows

type ReadWrite interface {    Read(b Buffer) bool    Write(b Buffer) bool}type Lock interface {    Lock()    Unlock()}type File interface {    ReadWrite    Lock    Close()}

The file interface contains the ReadWrite interface, the lock interface, and the method close, and any type that implements the file interface also implements the interface ReadWrite and lock

Points to be aware of

    • A type does not need to explicitly declare that it implements an interface, an interface is implicitly implemented, and multiple types can implement the same interface
    • One type can implement multiple interfaces
    • Implements the type of an interface and can have other methods

The difference between type T and t* in an interface method

type typeA struct {}type intfA interface {    func1()}func (a *typeA) func1() {}func func2(i intfA)  {    i.func1()}var a typeA

In the above example, when calling Func2 on a, a compiler error is caused because FUNC2 requires a INTFA, and its method func1 defined on the pointer.

The Go language specification defines the invocation rules for an interface method set:

    • the set of callable methods of type T contains all the set of methods for which the recipient is T or T
    • The set of callable methods of type T contains all the methods for which the recipient is T
    • The set of callable methods of type T does not contain the method that the recipient is *t

Summary

In summary, an interface is a set of methods. As long as a type implements a method in an interface, a variable of this interface type can accept a variable of that type, and the same interface can receive different types of variables.

Related Article

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.