This is a creation in Article, where the information may have evolved or changed.
The original plan is to write the function, the study of the function, and did not find any sentiment, so write the struct bar. This is a boy, a non-toy C system, is definitely a struct of the world, can go more've seen Sheng Bluetooth.
Structs are basically always in go with type, and Go's type keyword is similar to typedef in C, which defines an individual name for a variable.
First appreciate the code for a struct:
Package Mainimport "FMT"/** * defines the person class */type person struct {name String age int}/** * Defines a 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 girl class, inherited from Perso n */type Girl struct {person gender string}func (this *girl) Setgender (gender string) {This.gender = Gender}fun C (This *girl) Getgender () string {return This.gender}func main () {p1: = new (person) P1. SetName ("Yixiao") p1. Setage (FMT). Printf ("Name:%s, Age:%d\n", p1. GetName (), p1. Getage ()) var p2 person P2. SetName ("Wxw") P2. Setage (23°c) fmt. Printf ("Name:%s, Age:%d\n", p2. GetName (), p2. Getage ()) var g Girl G.setname ("Lili") G.setage (a) G.setgender ("Girl") fmt. Printf ("Name:%s, Age:%d, Gender:%s\n", G.getname (), G.getage (), G.getgender ())}
I believe Java and C + + programmers see this piece of code as a sense of déjà vu. The most basic object-oriented approach is to provide the method of operation on top of the encapsulated data. While go supports certain object-oriented programming, we have to be clear that it is not an OO language, just an imperative language, like C. The Mian function of the above code uses two methods to define the person object, the first is the pointer, the second is the variable, but both methods can access its method in the same way, which is allowed by the go language, and you may be surprised if you are a C programmer.
The above code also has the inheritance relationship, this is the basic object-oriented programming technology, but I personally tend to avoid using inheritance, can not be used, all go support inheritance does not let me get excited about it, but language-level support for the encapsulated data classes to provide methods is very good, which satisfies a large part of the C The class C + + is written by the person who has the C. program. As long as they are simple data and methods of encapsulation, not polymorphic, not even generics and so on.
(Correction: The preparation here should not be inherited, it should be a combination of types, although the effect is similar to inheritance.) I remember that C + + programmers often argue whether to use inheritance or combination. )
A qualified C + + programmer is definitely not writing C programs in C + +, they need to be familiar with all kinds of miracle masturbation and need to know the memory distribution of C + + class. A complex class, you just give him a pen and a piece of paper, he can draw you all kinds of member variables, various methods, virtual functions, pure virtual function memory distribution, and also can clearly explain the performance of virtual function table and other issues. To this day, I have already the virtual function, pure virtual function, even the virtual function of the table and so on to forget worked two. These things are not used to help you write elegant programs, but to hinder you, so that you can not understand the small elegance of life.
The essence of object-oriented is abstraction, and the result of being over-abstracted is lost.