Golang Object-Oriented programming

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

Overview

The object-oriented language of Golang is different from that of c++,py, because Golang does not support inheritance; Unlike the above object-oriented languages that support aggregation and inheritance, Golang only supports aggregations (also called combinations) and embedding. Differences in aggregation and embedding:

type ColoredPoint struct {    color.Color     //匿名字段(嵌入)    x, y int        //具名字段(聚合)}warning:(point := ColoredPoint{})    字段访问:        point.x , point.y, point.Color [当访问来自于其他pkg的类型字段时候,只用到了其名字的最后一部分]

In traditional object-oriented programming, "Class", "Object", "instance (instance)" are defined very clearly. In Golang there is no such terminology at all, instead of using "type" and "value", where the value of the custom type can contain methods;

Since there is no inheritance in Golang, there is no virtual function. Golang supports this with type-safe duck type (duck type). A simple overview is: In Golang, parameters can be declared as a specific type (for example, int,string, or *os. File and MyType), or an interface (interface), which provides a value that has a method that satisfies the interface.

对于一个声明为接口的参数,可传入任意值,只要该值包含该接口所声明的方法。无论该值的实际类型是什么;

This is exceptionally flexible and powerful, especially when combined with the method of accessing embedded fields supported by Golang;

Replace inheritance

The advantage of inheritance is that some methods are implemented once in the base class and can be used in subclasses; Golang provides two solutions for this:

    • use embedding; embedding a type, the method value needs to be implemented once in the embedded type and can be used in all types that contain the embedded type;
    • provide a separate method for each type , simply wrap the functional code into a function, and then have all of the class's methods call this function;

Golang Interface

Another distinguishing point in Golang object-oriented programming is its interface, where values and methods are kept separate.

    • Interface for declaring method signatures
    • Structs are used to declare aggregated or embedded values
    • Method is used to declare an operation on a custom type (usually a struct)

There is no contact between the method of the custom type and any special interface. However, if a method of this type satisfies one or more interfaces, the value of that type can be used wherever the value of the interface is accepted. Of course, each type satisfies the null interface (interface{}), so any value can be used to declare an empty interface;

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.