Go language infrastructure structure parameters

 3 minutes to read

go language infrastructure parameters

Use the struct keyword to define a structure, and the members of the structure are called fields or properties of the structure.

type Person1 struct {
Name string
Age int
}

Directly define variables. This method of use does not assign initial values ​​to fields, so all fields will be automatically assigned the zero value of their own type. For example, the value of Name is an empty string “”, and the value of Age is 0.

var p1 Person1
fmt.Println(p1)

Like arrays, structures are passed by value. For example, when an array or structure is passed as an actual parameter to the formal parameter of a function, a copy will be made. Therefore, in order to improve performance, the array is generally not passed directly to the function, but Instead of using slices (reference types), you can use pointer structures when passing structures to functions.

Pointer structure, that is, a pointer to a structure. When declaring a structure variable, add a * sign before the structure type to declare a pointer to the structure

var p2 *Person1
p2.Name="Hello"

Using Go’s built-in new() function, you can allocate memory to initialize the structure, and return a pointer to the allocated memory. Because it has been initialized, you can directly access the fields.

p3:=new(Person1)
p3.Name="Hello"
fmt.Println(*p3)

If the structure is passed to the function, it just copies a copy of the structure. If the field value of the structure is modified within the function, the external structure will not be affected, and if the structure pointer is passed to the function, it is used in the function. Modifications made by a pointer to a structure will affect the structure pointed to by the pointer.

package main

import "fmt"

type Person1 struct {
	Name string
	Age  int
}

func main() {
	var p1 Person1
	fmt.Println(p1)
	//var p2 *Person1
	//p2.Name="Hello" //空指针 未初始化 p2为nil
	p3:=new(Person1)
	p3.Name="Hello"
	fmt.Println(*p3)

	p := Person1{Name: "John", Age: 18}
	fmt.Printf("before change pointer address %p\n", &p)
	noModify(p)
	fmt.Println(p.Age, p.Name)
	modifyPerson(&p)
	fmt.Println(p.Age, p.Name)
}

func noModify(p Person1) {
	fmt.Printf("noModify's pointer %p\n", &p)
	p.Name = "bruce"
	p.Age = 19
}

func modifyPerson(p *Person1) {
	fmt.Printf("modifyPerson's pointer %p\n", p)
	fmt.Printf("modifyPerson p's pointer %p\n", &p)
	p.Name = "bruce"
	p.Age = 19
}

The results are as follows:

{ 0}
{Hello 0}
before change pointer address 0xc00000c060
noModify's pointer 0xc00000c078
18 John
modifyPerson's pointer 0xc00000c060
modifyPerson p's pointer 0xc00000e030
19 bruce

According to the running results, it can be seen that the memory addresses before and after the structure is passed parameters are different, the structure pointer is passed to the function, and the value in the pointer structure is the address of the actual parameter structure, so change The value of the formal parameter will also change the value of the actual parameter.