This is the 16th day of my participation in Gwen Challenge

These reviews

Previous articles focused on the use of functions and interfaces in the Go language. Similar to C, Golang provides structure types. As a compound type, a structure consists of multiple fields, each of which has its own type and value. Structures and fields can be understood as entities and their corresponding attributes. In Golang, not only structures can have methods, but every custom type can have methods.

This article will begin by introducing structures and methods. A structure is a collection of data with the same or different types of data. So let’s start with the definition of a structure.

Definition of structure

With the type and struct keywords, you can customize the structure. Golang’s Type keyword allows you to define a variety of basic types as custom types, and multiple basic types and constructs can be compounded in structures for easier use. The struct definition looks like this:

type structName struct{
	value1 valueType1
	value2 valueType2
	...
}
Copy the code

Structure names cannot be repeated within the same package. If you want the structure to be accessible outside the package, the initial letter of the structure needs to be the size. The field name in the structure must be unique. If the field is public, the first letter of the field name must also be capitalized.

Let’s try to define a simple structure Person with fields like name, date of birth, ID number, and so on, as follows:

type Person struct {
	Name string	/ / name
	Birth string	/ / birthday
	ID int64	// Id number
}
Copy the code

Instantiation and initialization of a structure

Instantiation allocates specific memory for storing fields to a structure that must be instantiated before it can be used. Instantiation creates a consistent memory region for the structure to store the structure based on its definition. Each instance of the structure is independent of each other.

There are many ways to instantiate a structure. For example, we can instantiate a structure as if it were a primitive type, and then assign values to its fields. For example, the Person structure defined in the previous section looks like this:

// Declare instantiation
var p1 Person
p1.Name =  "Wang Xiaoer"
p1.Birth = "1990-12-11"
Copy the code

We can also use the new function to request a memory region for the structure. This will return the pointer type for the structure. Assign the fields of the structure as follows:

// Instantiate the new function
p2 := new(Person)
p2.Name = "Wang Erxiao"
p2.Birth = "1990-12-22"
Copy the code

In addition, the structure is addressed with &, which is treated as a new instantiation and returns the pointer type as follows:

// address instantiation
p3 := &Person{}
p3.Name = "Wang SAN Xiao"
p3.Birth = "1990-12-23"
Copy the code

During the struct instantiation process, we can initialize the fields in the struct body, which can be populated using jSON-like key-value pair representations. Initialization of fields is optional. Again using the Person structure as an example, the code looks like this:

/ / initialization
p4 := Person{
	Name:"Wang Xiao Si",
	Birth: "1990-12-23",}Copy the code

When all fields in the body of the structure need to be initialized and the fields are filled in the same order as they were defined in the body of the structure, the keys in the key-value pair can be omitted as follows:

/ / initialization
p5 := &Person{
	"Fifty"."1990-12-23".5,}Copy the code

The above example uses an address instantiation that returns a pointer of type P5 to the Person structure, i.e. *Person.

summary

This paper mainly introduces the definition of structure and instantiation and initialization of structure. Arrays can store the same type of data in Go, but in structures we can define different data types for different items. As a compound type, a structure consists of multiple fields, each of which has its own type and value. Structures and fields can be understood as entities and their corresponding attributes.