Interface related knowledge in go language

Source: Internet
Author: User
Tags inheritance lua

The birth of a piece of work is usually done by a designer independently. Because of this, a building or a painting or a dance of music can truly reflect its individuality. And it is this unique aspect that differs from the others, and it is the novelty and practicality of the creator that does not deviate from the creative purpose and the original structure that makes innovation particularly rare.

The birth of the Go language was done by three designers with strong personalities. The go language positioning, like a point in a three-dimensional coordinate system, represents a projection of the creative thinking of Ken, Robert, and Rob on the three dimensions of strong type, dynamic, and concurrency.

Of course, this description is not only to express the three features of the Go language, but also to make it clear that the three features are orthogonal, that is, they are independent of each other, so they can be used at the same time without constraining each other. Of course, this description is only an image metaphor, not that the three designers are independent of each other and do not have to control each other, you can get the same independent complete go language architecture. On the contrary, only the features that are shared by three of people will appear in the Go language specification before they are published in the Go language implementation. Interestingly, the design mix of the troika is also used in the Lua language. It allows LUA to successfully avoid an overly designed trap that keeps itself slim while not staying clean, but constantly updating itself and improving performance.

If one of the features of the Lua language is its unique but flexible and efficient table composite type, one of the features of the go language is that I think is the only but flexible and efficient interface dynamic type. This type allows the go language to be flexible and safe to convert between compatible types while maintaining a strong, static type of security and efficiency. Before we get to the point, we'll start with an easy topic. As for the Chinese translation of interface, Orthodox education teaches us that it is an interface. For example, objects in Java and C + + can be understood as very autistic individuals or the genealogy of similar individuals with the same genetic genes. At this point, the interface can properly say: to get my genetic genes, you have to use this interface. For example, only those types that claim to be in connection with donkey horses can claim to be mules. Interfaces are explicitly declared when the class is defined.

In the go language, "kissing needs to be stated (note that the mouth is small to make it funny to enhance memory)". So the go interface is completely different from the Orthodox class. To avoid misunderstanding, and in order to and the international translation of Hong Kong and Macao, I tend to translate interface into "interface". Of course, this is also in line with the etymology of the English language: the Inter is the face of the middle boundary.

All right. That's enough. It's time to say go.

To understand dynamic types, you need to start with static. Go, like the C-family language, is a strongly static type of compiled language. Each variable must declare its type in advance, and only variables of the same type can be assigned and participate in the operation. For example:

   I: = 0
    J: = 0i

Declare the variables I and J are integer int and plural type complex128 respectively. Although they are all zeros, the go compiler is bound to be strongly opposed, even though we are convinced that the two zeros can be added and should get the correct 0. It is considered that I and J are not a class that cannot be calculated. This is strongly static type compilation. It makes the things that programmers think can be done meticulous mandatory type checking, usually do not conform to its provisions are not compiled, but to report errors for the author to review themselves.

If the author wants to bargain with the compiler, he must read the Go language specification like a lawyer to understand what is flexible and what is absolutely forbidden. For example, the Go Language specification provides for a limited number of reciprocal conversions between numeric types, such as between integers and floating-point numbers, but not to complex numbers. If J: = 0.0 declares that j is a floating-point number type, then float64 (i) + J can make an addition operation between the same type of variable after forcing the integer I into the floating-point type.

Readers who have learned object-oriented programming might think: Hey, go. If you can support operator overloading or inheritance like the xxx language, there is no problem with this addition type incompatibility.

Is that a problem? Or does it mean that the problem has been abstracted and obscured or that scholars have learned a different level of knowledge besides learning different types? Is it simpler or more complex and harder to figure out? Trust the reader to be discerning.

Go object-oriented does not support overloading and has only limited inheritance. The purpose is clear: Go is a type system that simplifies type systems, especially object-oriented classes that have been overly complicated.

Does this have anything to do with the dynamic type system that the interface represents? Or we ask ourselves, how can the problem of object-oriented complex classes and type systems be addressed in the go language? Static classes and types, can dynamic interface?

For example, to implement the addition of the area of two different types of shapes, in an object-oriented language, you need to define a base class, so that there is a way to add the ribs, and then let each shape inherit, before you can To let the compiler know that the type of the shape of these classes inherits the shape that is not of any particular shape declares an operation without any specific operation of the obtained area, so that it can be accommodated, so that the specific type must have been redefined by the specific method of obtaining their own area to obtain specific values, can be two The sum of the values of the same kind is added to obtain the sum of the area.

If scholars think that the author deliberately put a simple truth Yunshan, that scholar comrades really understand the object-oriented spirit. Let's dispel and see how the Go interface explains the operation. "The kiss needs to be declared" or "the interface does not need to be stated". For example, as long as two shapes have a method of taking area, they can add up their area, so simple and clear that they do not need to organize them into similar abstract shapes, and they cannot do this in the go. Specific examples:

Package main
 
Import "FMT"
 
type square struct{r int}
type circle struct{r int}
 
func (S Square) area () I NT {return S.R * S.R}
func (c circle) area () int {return C.R * 3}
 
func main () {
    s: = Square{1}
    c: = CI Rcle{1}
    FMT. Println (S, C, S.area () +c.area ())
}

The so-called interface here is a method of square squares and circular circle with area () Int. Notice that we want to use the interface type below:

Package main
 
Import "FMT"
 
type square struct{r int}
type circle struct{r int}
 
func (S Square) area () I NT {return S.R * S.R}
func (c circle) area () int {return C.R * 3}
 
func main () {
    s: = Square{1}
    c: = CI Rcle{1}
    A: = [2]interface{}{s, c}
    FMT. Println (S, C, a)
 
    sum: = 0 for
    _, T: = range A {
        switch v.: = T. (type) {case
        square:
            sum = V.area () Case
        circle:
            sum + = V.area (
        )
    }
    fmt. Println (sum)
}

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.