“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

The structure of the body

In contrast to Java, the Go language has no concept of classes, but more structures. A structure, much like a class in Java, is a collection of data that represents a series of data of the same or different types.

For example, a student can be abstracted into a structure, with each student having the following properties:

Name: Name

Age: Age

Gender: Gender

Grade Grade:

Then all of the above attributes form a structure that can be named Student.

Structure definition

When using a structure, the structure needs to be defined first. Keywords type and struct are used to define the structure. The syntax is as follows:

typeStructure namestruct{member name Member type}Copy the code

Examples of code for the Student structure:

// Define a Student structure
type Student struct {
	Name string
	Age int
	Gender string
	Grade string
}
Copy the code

As shown above, a Student structure is defined with member variables like Name, Age, and so on.

Structure declaration

You can specify the values of member variables in a structure directly when you declare a structure.

Code examples:

package main

import "fmt"

func main(a) {
	/ / 1
	stu1 := Student{"Zhang".18."Male"."Freshman"}
	/ / 2
	stu2 := Student{
		Name: "Bill",
		Age: 20,
		Gender: "Female",
		Grade: "Junior",}/ / 3
	stu3 := Student{}
	fmt.Println(stu1)
	fmt.Println(stu2)
	fmt.Println(stu3)
}

// Define the structure Student
type Student struct {
	Name string
	Age int
	Gender string
	Grade string
}
Copy the code
  1. Use this method to declare a structure and specify the values in the order of the member variables in the structure. It is important to note that this method requires the assignment of all member variables, not only parts of them.
  2. This method assigns values to member variables in the form of key-value pairs. You can assign values only to specified member variables, and default values to others that are not assigned.
  3. This method declares only one structure and does not assign values to member variables, so all variables in the structure are default values.

Running results:

Access structure members

In the above code, is directly output the entire structure, if you need to access a member variable in the structure, or change the value of a member variable already declared, need to use. Operators.

Code examples:

package main

import "fmt"

func main(a) {
	// Declare a structure and assign a value
	stu := Student{"Zhang".18."Male"."Freshman"}
	// Prints the value of the structure
	fmt.Println("Student name:", stu.Name)
	fmt.Println("Student Age:", stu.Age)
	fmt.Println("Gender of student:", stu.Gender)
	fmt.Println("Grade:", stu.Grade)
	// Modify the value in the structure
	stu.Name = "Bill"
	stu.Age = 19
	stu.Gender = "Female"
	stu.Grade = "Big"
	// Prints the value of the modified structure
	fmt.Println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --")
	fmt.Println("Student name:", stu.Name)
	fmt.Println("Student Age:", stu.Age)
	fmt.Println("Gender of student:", stu.Gender)
	fmt.Println("Grade:", stu.Grade)
}

// Define the structure Student
type Student struct {
	Name string
	Age int
	Gender string
	Grade string
}
Copy the code

Running results:

As shown above, use. The operator allows you to easily access or modify a member variable in a structure.

methods

In the article to learn the language before function, in fact there are both function and method in the language, the way is to set the recipient’s function, the receiver is in front of the function name one more parameter, said it would this method to bind to the parameters of the corresponding type, this method can only be after binding the parameters corresponding to the type of variable to invoke.

If you have the following method, it can only be called on variables of type Student by the name of the variable. The method name.

func (stu Student) print(a)  {
	/ / the method body
}
Copy the code

Code examples:

package main

import "fmt"

func main(a) {
	// Declare a structure and assign a value
	stu := Student{"Zhang".18."Male"."Freshman"}
	stu.print()}type Student struct {
	Name string
	Age int
	Gender string
	Grade string
}

func (stu Student) print(a)  {
	fmt.Println("Name", stu.Name)
	fmt.Println("Age", stu.Age)
	fmt.Println("Gender", stu.Gender)
	fmt.Println("Grade", stu.Grade)
}
Copy the code

In the above code, we define a print method whose receiver is Student, so this method is bound to Student, so that only variables of Student can be called, so there is no problem calling this method with stu.print() in main. But you can’t call the method directly from main, and when you call the method using STu, you get the values of the stU member variables inside the method.

Running results:

Structure pointer

A structure, as a type, also has a pointer type. For example, the Student structure in the above example has a pointer type *Student, which can declare a pointer to store the address of the structure variable.

var stuPointer *Student
Copy the code

The address of the struct type variable is then assigned to the pointer stuPointer by fetching the address symbol & fetching.

stuPointer = &stu
Copy the code

This can also be used when using structure Pointers. The operator accesses member variables inside the structure.

Code examples:

package main

import "fmt"

func main(a) {
	// Declare a structure and assign a value
	stu := Student{"Zhang".18."Male"."Freshman"}
	// Define the structure pointer
	var stuPointer *Student
	// Assign the structure address to the pointer
	stuPointer = &stu
	// Struct pointer passes point (.) The operator accesses a member variable
	fmt.Println("Name:", stuPointer.Name)
	fmt.Println("Age:", stuPointer.Age)
	fmt.Println("Gender:, stuPointer.Gender)
	fmt.Println("Grade:", stuPointer.Grade)
}

type Student struct {
	Name string
	Age int
	Gender string
	Grade string
}
Copy the code

Running results:

You can also add using the structure pointer. Operator to modify a member variable.

Stay tuned for updates…

Public number: good ape, pay attention to view more articles and dry goods, reply to Golang receive golang learning ebook.