About the Go language learning experience 2:2017-1-8

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

PS: I have time to study on weekends, so I even remember two notes.

About interfaces in the go language: assigning values to interfaces

The interface of GO is very important according to the introduction of "Go Language programming".

On the implementation of the interface, there is a concept: in theGo language, a class only needs to implement the interface requirements of all functions, we say that this class implements the interface (emphasis)

For example, there is an interface (this does not want to use the original book example, but the description is really simple)

Type integer int//a new int-based integer

Type MyInterface Interface {//This is an interface this is a go language interface Ah, this interface is long this virtue AH

Less (b integer) integer

ADD (b Intteger)

}

The next step is to implement this interface, which is conceptually

Func (a integer) less (b integer) integer{//parameter type description: (a integer), which represents the method binding on the type Integer, equivalent to providing a class method to a class; less: Method name; b: Formal parameter name

Do something ...

}

Func (a *integer) Add (b Integer) {

Do a little pointers on what to do.

}

The focus is on the process of using


Func Main () {//very much like C's main function Yes, really like

var a Integer = 1

var b Integer = 2

var c myinterface = &a//As for why a reference to a value, not a value, is to maintain the implementation of the interface parameter type unification, as for the reason reference "Go ..." You know this book

C.add (b)//This step is crucial to my small white, almost forgot c This variable value, is not an integer type, it is a variable of the interface type

Above this is called C interface, dry one thing, is to use the Add this method, and with B this variable took place some ambiguous things, can Lenovo Cang teacher ...

C This interface finished the Lenovo teacher's thing, the beneficiary, is the variable A, because C uses a reference address

Soso

Fmt. Println (a)

This step you will get a value of 3, this implementation process, from the interface MyInterface

}


PS: Ms. Cang is also a goddess ...

PS: "Go language Programming" is my main reference books, so later this book referred to as "Go ..." for the simple philosophy of programming salute

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.