Follow the old cat to GO, "object oriented"

preface

I shared the container and related basic syntax and functions with you before. I believe that if you have friends who have been in contact with C + + or java, you know object-oriented. In fact, object-oriented also exists in GO language, but it is still relatively simple. Let's take a look at the "Object-Oriented" in GO language.

object-oriented

Definition of structure

In fact, go language can not be accurately said to be object-oriented. Go language is actually a language for interface function programming. Why should we say object-oriented in go language? In fact, some features are similar to object-oriented. The object-oriented in go language is relatively simple. Go language only supports encapsulation and does not support polymorphism and inheritance. Language has no class, only struct.

A structure is essentially a data type. It is an aggregate data type that combines 0 or more named variables of any type. Each variable is called a member of the structure. Let's see the definition of structure, as follows

type Employee struct {
	ID int //id number
	Name string //name
	Address string //address
	Position string //position
}

func main() {
	var jack Employee
	jack.Name = "jack"
	jack.Address = "shanghai"
	fmt.Println(jack.Name)
}

Above, we define an Employee structure, which contains some attributes such as name, address, position, etc. In the main function, we create a jack of type Employee and give it an initialization value. In the above simple way, we can directly use the "." method to assign and value the structure variable. Of course, we can also obtain the address of the member variable and access it through the pointer. As follows:

func main() {
	var jack Employee
	jack.Name = "jack"
	jack.Address = "shanghai"
	jack.Position = "Engineer"
	//fmt.Println(jack.Name)

	position := &jack.Position
	*position = "Senior" + *position
	fmt.Println(*position)
}

Obviously, jack here has been appreciated and become a senior engineer.

In fact, our dot can also be directly used on the pointer of the structure, as follows

func main() {
	var jack Employee
	jack.Name = "jack"
	jack.Address = "shanghai"
	jack.Position = "Engineer"
	//fmt.Println(jack.Name)

	position := &jack.Position
	*position = "Senior" + *position
	//fmt.Println(*position)

	var employeeA *Employee = &jack
	employeeA.Position = "Super" + employeeA.Position
	fmt.Println(employeeA.Position)
}

If you don't understand the pointer thoroughly, you may be a little confused. The old cat in the back still talks about the pointer. In the above step, we only obtained jack's position and reassigned and upgraded it through the pointer. Next, in fact, we defined an Employee pointer, and this pointer points to jack's structure. Then, for our Employee pointer, Employee a, we can obtain all the attributes in its structure and reassign it.

Of course, we can even define pointer type structure functions. Of course, the return value must be the address of a structure. The specific definitions are as follows:

func EmployeeByID(id int) *Employee {
	var json Employee
	json.ID = id
	json.Name = "json"
	json.Address = "beijing"
	json.Position = "Engineer"
	return &json
}

Above, we mainly introduced the usage of structures and pointers. What else should we pay attention to about structures?

  • The order of member variables is very important for structural identity. If we reverse the order of the attributes of the above Employee structure, we say that another structure of different types is defined.
  • As for the case of variables defined in the GO structure, you can see that the above definitions of the old cat start with a capital letter, because only the attributes defined with a capital letter can be accessed by the periphery. You can manually tap the code to experience it. This is also the main access control mechanism of GO.
  • A structure type cannot define a member variable with the same structure type s, that is, an aggregate type cannot contain itself.

What does this mean? Let's look at an example!

In the figure above, we can see that if the structure has its own set, a compilation error will be reported. However, an S pointer type can be defined in Employee. For example, the following is OK

type Employee struct {
	ID int
	Name string
	Address string
	Position *Employee
}

So we can use this form to define the recursive structure, such as the definition of linked list or tree

type tree struct {
	value int
	left,right *tree
}

Literal of structure

The literal here is the value in the structure. The value in our structure type can be set through the literal of the structure. As follows, there are two structural literals.

First kind

type Point struct {X,Y int}
p:=Point{1,2}

This method requires assigning values to each member variable in the normal order. Obviously, it doesn't matter if the structure is relatively simple, but once the structure becomes quite complex with the evolution of the business, the maintainability of the code becomes quite poor.

Second

type Point struct {X,Y int}
p:=Point{X:1,Y:2}

This method is obviously clearer, but it should be noted that if a member variable does not have a specified value, its value is zero by default. Since the name of the member variable is specified, it is equivalent to the first in this way, and the order here doesn't matter.

Comparison of structures

If all member variables of a structure can be compared, the structure can be compared. For the comparison of two structures, use = = or= Just.

p:=Point{1,2}
q:=Point{2,1}
e:=Point{1,2}
fmt.Println(q.x == p.x) //Member function comparison false
fmt.Println(p == q) //Overall comparison false
fmt.Println(p == e) // true

In object-oriented languages, such as java, when we compare the values of two objects, we need to compare the hash values of two objects, and even rewrite the equals method. The comparison of structural objects of go language we see here is very simple and clear. I don't want to repeat here. I still hope you can write more.

Nesting mechanism of structure

The nesting mechanism of structures allows us to use a named structure as an anonymous member of another structure type. This sentence may be a little difficult to understand. Let's take a direct look at the example.

First, let's define a circle. A circle contains the coordinates of the center of the circle and the relevant radius. From this, we can abstract the following code

type Circle struct {
	X,Y,Radius int
}

In our daily life, the wheel is also round, and the wheel may have more banners. Therefore, we can also abstract the wheel

type Wheel struct {
    X,Y,Radius,Spokes int
}

Seeing the above two, we can actually find that there are many same member variables in the two structures. Can we abstract them again, so we abstract them as follows:

type Circle struct {
	Center Point
	Radius int
}

type Wheel struct {
	Circle Circle
	Spokes int
} 

In this way, we will find that the whole program looks clearer. In fact, this better illustrates that the structure is also a special data type.

Write at the end

In this article, we have roughly shared the relevant knowledge of structures. Small partners with experience in java related object-oriented language will find that structures are similar to classes in object-oriented language, but the usage of structures in GO language will be much simpler.

I am a cat, more content. Welcome to search for official account of cat.

Tags: Go

Posted on Tue, 30 Nov 2021 10:53:15 -0500 by Charles Wong