Go language learning notes 4 -- struct

Source: Internet
Author: User

The original plan was to write a function. I did not find any insights on the research functions I used. So I would like to write struct. This is a good guy. In a non-toy C system, it is definitely the world of struct, but go is more blue than blue.

Struct usually appears together with type in go. The type keyword of Go is similar to typedef in C, and an alias is defined for a variable.

First, let's look at a piece of struct'sCode:

Package mainimport "FMT"/*** defines the person class */type person struct {name string age int}/*** defines the method for the person class */func (this * person) setname (name string) {This. name = Name} func (this * person) getname () string {return this. name} func (this * person) setage (age INT) {This. age = age} func (this * person) getage () int {return this. age}/*** defines the girl class, inherited from person */type girl struct {person gender string} func (this * Girl) setgender (Gender string) {This. gender = gender} func (this * Girl) getgender () string {return this. gender} func main () {p1: = new (person) p1.setname ("yixiao") p1.setage (25) FMT. printf ("Name: % s, age: % d \ n", p1.getname (), p1.getage () var P2 person p2.setname ("wxw") p2.setage (23) FMT. printf ("Name: % s, age: % d \ n", p2.getname (), p2.getage () var g girl G. setname ("Lili") g. setage (12) g. setgender ("girl") FMT. printf ("Name: % s, age: % d, Gender: % s \ n", G. getname (), G. getage (), G. getgender ())}

I believe that Java and C ++ProgramYou can see that this piece of code is absolutely familiar. The encapsulated data can also provide operation methods, which is the most basic object-oriented. Although go supports certain object-oriented programming, it must be clear that it is not an OO language, but an imperative language, just like C. The mian function in the code above uses two methods to define the person object. The first is actually a pointer, and the second is a variable, however, both methods can be accessed in the same way. This is allowed by the go language. If you are a C programmer, you may be surprised.

The above Code also has an inheritance relationship, which is the basic technology of object-oriented programming, but I personally prefer to avoid using inheritance as much as possible, I was not excited by the inheritance of all go support. However, language-level support provides very good methods for encapsulated data classes, this satisfies a large number of people who use C ++ classes to write C Programs. Because they only need to encapsulate simple data and methods, and do not require Polymorphism or generics.

(Correct: the preparation here should not be an inheritance, a combination of components and types, although the effect is similar to inheritance. I remember C ++ programmers often argue about whether to use inheritance or combination .)

A qualified C ++ programmer does not use C ++ to write a C program. They need to be familiar with all sorts of miraculous tricks and be familiar with the memory distribution of C ++ classes. For a complex class, you only need to give him a pen and a piece of paper, and he can draw out the memory distribution of various member variables, methods, virtual functions, and pure virtual functions for you, the performance and other issues of the virtual function table can also be clearly described. Today, I have forgotten the full potential of virtual functions, pure virtual functions, and even virtual function tables. These things are not designed to help you write elegant programs, but to block you and make you unable to understand small elegance for a lifetime.


The essence of object-oriented is abstraction. The result of over-abstraction of all things is lost.



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.