The Magic Go language (object oriented)

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


"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Friends who have had a C + + language learning experience know that object-oriented mainly includes three basic features: encapsulation, inheritance, and polymorphism. Encapsulation, that is, the running of data and functions are bound together, C + + is mainly through the this pointer to complete; Inheritance means that classes can inherit attributes and functions from each other, polymorphism, which is mainly to use a unified interface to deal with common logic, Each class only needs to implement its own callback function according to the interface.


As a master of the Go language, Nature does not act on object-oriented. Compared to C + +, Java, C # and other object-oriented languages, its object-oriented is simpler and easier to understand. Below, we might as well use three simple examples to illustrate what object-oriented is in the go language.


Encapsulation features,

Package Mainimport "FMT" type data struct {val int}func (p_data* data) set (num int) {p_data.val = Num}func (p_data* data) Sho W () {FMT. Println (P_data.val)}func main () {p_data: = &data{4}p_data.set (5) p_data.show ()}
Inheritance attributes,

Package Mainimport ' FMT ' type parent struct {val Int}type child struct {parentnum Int}func main () {var c childc = Child{par Ent{1}, 2}fmt. Println (C.num) fmt. Println (C.val)}
Polymorphic Properties,

Package Mainimport "FMT" type Act interface {write ()}type xiaoming struct {}type xiaofang struct {}func (xm *xiaoming) writ E () {fmt. Println ("Xiaoming write")}func (XF *xiaofang) write () {FMT. Println ("Xiaofang write")}func main () {var w act;xm: = xiaoming{}xf: = Xiaofang{}w = &xmw.write () w = &xfw.write ()}


On the object-oriented, the go language behaves more concisely and directly. On the one hand, this comes from a thorough understanding of object-oriented by the language designer, and on the other hand, I think it comes from the Google Designer's summary of past experience. Personally feel that these characteristics are very interesting, interested friends can try for themselves.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.