The structure of the body

Structs are one of the more important concepts in GO, and there are similar ones in C. Since they do not have the concept of a class, a structure can simply be understood as a class, which is a collection of different types of data. The different types of data in the collection are called members, and each member has its own different type, which can be understood as each attribute of the object in JS.

Declarative structure

Structures are declared using the type and struct keywords, which are followed by the name of the structure and the definition of each member of the structure.

type Person struct {
  name string
  age int
  gender string
  address string
}
Copy the code

The above code is somewhat similar to the definition of interfaces in other languages. In fact, go also supports defining interfaces. We just need to replace the struct keyword with interface to define the interface.

Initialize the structure

There are two ways to initialize a structure. One is to initialize a structure literal, using the name of the structure plus the values of each member of the structure. For example, use the Person structure above:

var p = Person{"Shenfq".25."Male"."Changsha, Hunan"}
fmt.Println("Person:", p)
Copy the code

This approach requires each value to be initialized in the order in which the structure members were defined, or, of course, scrambled by means of key-value pairs. In this way, some members can be omitted, and the omitted part will take the null value of the type according to its type.

var p = Person{
  name: "Shenfq",
  address: "Changsha, Hunan",
}

fmt.Println("Person:", p)
fmt.Println("Person.age:", p.age)
Copy the code

If you want to access structure members, you can use the. Operator, which is consistent with how other languages fetch object attributes. Here we use p.age to get the value of the member age of structure P.

In addition to being initialized by literals, structures can also be initialized by the new keyword.

var p = new(Person)
Copy the code

The structure initialized in this way has two characteristics:

  • The new keyword returns a structure pointer;
  • The new keyword returns a null value for each member;

So, when we initialize a structure with new, we need to add an asterisk to the value.

var p = new(Person)
p.name = "Shenfq"
p.age = 18
p.gender = "Male"
p.address = "Changsha, Hunan"

fmt.Println("Person:", p)
Copy the code

If you print the variable p directly on the console, you’ll notice that it’s preceded by an ampersand, indicating that it’s a pointer.

Anonymous structure

A structure, like a function, can also define a structure without a name, that is, it is initialized at the same time as the structure is defined, and the type keyword and the structure name are omitted.

var p = struct {
  name string
  age int
  gender string
  address string
} { "Shenfq".25."Male"."Changsha, Hunan"}
Copy the code

methods

A structure can define only one member, and the members are basic types. To implement concepts similar to OOP classes, you need to provide methods for a structure. In fact, we can specify methods for a structure by prefixing the name of the function that defines the function to be the method of the structure.

We define a sayHello method for the previous Person structure.

func (p Person) sayHello(name string) {
	fmt.Printf("Hi %s, I'm %s, How are you? \n", name, p.name)
}
p.sayHello("Jack")
Copy the code

Struct methods are called in the same way that struct members are called. Operators.

In Goland’s Structure, it can be seen that the Person Structure contains the sayHello method, indicating that the method belongs to the Structure even if the definition of the method is not in the Structure.

Pointers in methods

Sometimes, while calling a method, we need to change the values of some members of the structure and find that the values of the original structure have not changed.

func (p Person) growth(a) {
	p.age++
}

var p = Person{ age: 25 }
p.growth()
Copy the code

In the code above, we define the growth method, which modifies the age value in the passed structure. But the actual results were not what we expected.

var p = Person{ age: 25 }

p.growth()
fmt.Println("age:", p.age)
Copy the code

This is because the structure in the method is the copied value of the original structure, and if you need to modify the original structure, you need to pass the pointer to the method. Just add an asterisk (*) when the method defines the structure parameter to indicate that the variable p is the structure pointer.

func (p *Person) growth(a) {
	p.age++
}
Copy the code