Go: struct structure type and pointer [study notes record]

struct structure type

Arrays in Go language can store data of the same type, but in structures we can define different data types for different items.

A structure is a collection of data consisting of a series of data of the same type or different types.

A structure represents a record. For example, all students can be regarded as a structure, and each student contains attributes such as name, gender, age, and student ID.

1. Define the structure

Structure definitions require the use of type and struct statements. The struct statement defines a new data type with one or more members in the structure. The type statement sets the name of the structure.

type 结构体名 struct {
    
    
   	// 成员变量
    成员变量名 数据类型
}

2. Access structure members

Accessing structure members in Go is also done by "struct. member variable name".

结构体.成员名

For example, create a student structure, then initialize and assign it, and then print out the detailed information of the corresponding structure

package main

import "fmt"

// 结构体的使用

// 1.struct 指明结构体类型 ,type 指明 数据类型
type student struct {
    
    
	name string
	sex  string
	age  int
	sid  int
}

func main() {
    
    
	// 结构体对象创建格式
	var st1 = student{
    
    }
	st2 := student{
    
    }
	printSt(st1)
	printSt(st2)
    // 赋值初始化
	st1.sex = "男"
	st1.name = "黄飞鸿"
	st1.age = 18
	st1.sid = 2020040413
    // 赋值初始化
	st2.sex = "男"
	st2.name = "叶问"
	st2.age = 18
	st2.sid = 2020040414
	printSt(st1)
	printSt(st2)
	// 声明并且同时赋值初始化
	var st4 = student{
    
    name: "李四", age: 22, sex: "男", sid: 2020040415}
	st5 := student{
    
    name: "张三", age: 22, sex: "男", sid: 2020040416}
	printSt(st4)
	printSt(st5)
}

// 打印结构体成员的函数
func printSt(st student) {
    
    
	fmt.Printf("学生信息:姓名%v\t,性别:%v\t,年龄:%d\t,学号:%d\t\n",
		st.name, st.sex, st.age, st.sid)
}

The output of the operation is as follows:

学生信息:姓名   ,性别:  ,年龄:0 ,学号:0
学生信息:姓名   ,性别:  ,年龄:0 ,学号:0
学生信息:姓名黄飞鸿     ,性别:男        ,年龄:18        ,学号:2020040413
学生信息:姓名叶问       ,性别:男        ,年龄:18        ,学号:2020040414
学生信息:姓名李四       ,性别:男        ,年龄:22        ,学号:2020040415
学生信息:姓名张三       ,性别:男        ,年龄:22        ,学号:2020040416

This is just a simple use of a single structure. Generally, we use structure nesting to better describe entities.

3. The use of structures and anonymous fields

Go supports the method of only providing the type without writing the field name, that is, an anonymous field, also known as an embedded field (in the above method, the structure member variable name corresponds to the data type one by one).

For example, create a structure of People, then People is a large range, and students are one of the structures.

package main

import "fmt"

type People struct{
    
    
	name string
    sex string
    age int
}

type Student struct{
    
    
    People // 匿名字段的使用,只写数据类型,不写字段名
    sid int
}

func main() {
    
    
	var st1 = Student{
    
    People{
    
    name: "叶问", sex: "男", age: 23}, 10001}
	printSt(st1)
}

func printSt(st Student) {
    
    
	fmt.Printf("学生信息:姓名%v\t,性别:%v\t,年龄:%d\t,学号:%d\t\n",
		st.name, st.sex, st.age, st.sid)
}

Of course, other ordinary fields in the structure can also use anonymous fields

In the composite structure, if there are fields with the same name, the field with the same name in the outer structure is accessed first. If you want to access the field with the same name in the inner layer, you need to access it in the form of inner structure.member variable.

type people struct {
    
    
    name string 
    sex string
}
type student struct {
    
    
    people 
    name string
    sid int
}

access to external duplicate member fields

结构体.成员变量字段

Access the internal duplicate member fields

结构体.内嵌结构体.成员变量字段

For example the following example:

// 创建一个struct结构体
var st1 = student{
    
    people{
    
    "张三","男"},"李四",10001}
// 访问外部重名成员字段
print(st1.name) // 李四
print(st1.people.name) //张三

pointer

The address-taking symbol of Go language is &, and if used before a variable, the memory address of the corresponding variable will be returned.

A pointer variable points to the memory address of a value. Like variables and constants, pointers need to be declared before they can be used.

1. Declaration and use of pointer variables

The format of a pointer variable declaration is as follows:

var 指针名 *数据类型

Pointer usage process:

  • Define pointer variables.
  • Assign a value to a pointer variable.
  • Access the value at the address pointed to in the pointer variable.

Add the * sign (prefix) before the pointer type to get what the pointer points to.

Use as follows:

package main

func main() {
    
    
	var a = 10
	println(a) // 10
	var p *int
	// 将指针p指向变量a
	p = &a
	// 对指针p指向的值进行更改,a的值也会发生更改
	*p = 11
	println(a) // 11
	println(*p) // 11
}

2. Definition and use of pointer array

The declaration definition format of pointer array:

var 指针数组名 [size] *数据类型

It is basically the same as the pointer variable declaration format, except that the pointer array has an extra array length.

// 定义一个整型数组,然后使用指针数组分别指向里面的每个数组元素
var numbers = []int {
    
    11,22,33}
var ptrs [3]*int
for i:= range numbers{
    
    
    ptrs[i] = &numbers[i]
}
for j:= range ptrs{
    
    
    println(*ptrs[j])
}
// 11
// 22 
// 33

3. Function parameter modification value

Or the characteristics of the pointer, pointing directly to the value. The function passing parameters to modify the value is actually just passing in the address when calling the function.

For example, one swaps the values ​​of two variables

package main

import "fmt"

func main() {
    
    
	var a int = 10
	var b int = 20
	fmt.Printf("a:%d\t,b:%d\t\n", a, b)
	swap(&a, &b)
	fmt.Printf("a:%d\t,b:%d\t\n", a, b)
}

func swap(ptr1 *int, ptr2 *int) {
    
    
	var temp int
	temp = *ptr1
	*ptr1 = *ptr2
	*ptr2 = temp
}
// a:10    ,b:20
// a:20    ,b:10

Guess you like

Origin blog.csdn.net/m0_63622279/article/details/129427913