【GO Programming Language】Pointer and Structure

Pointers and Structs



insert image description here


1. Pointer concept

Pointers in Go language are easy to learn, and using pointers in Go language can perform some tasks more simply.

指针是存储另一个变量内存地址的变量
We know that a variable is a convenient placeholder for referring to a computer memory address.
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

package main

import "fmt"

func main() {
    
    

	var a int = 6

	// b 是指向了 a 的内存地址, & 符号为取地址符
	var b = &a

	fmt.Println("变量a的值:", a)
	fmt.Println("变量a的内存地址值:", &a)
	fmt.Println("变量b的值:", b)

	// *指针
	fmt.Println("变量b指向的内存地址中存储的值:", *b)

	*b = 8
	fmt.Println("变量a的值:", a)

}

insert image description here
The memory address value of variable a: 0xc00001c0a8, the value is 6
pointer variable b, pointing to the memory address 0xc00001c0a8
pointer variable b has the address of variable a, that is, b points to a

2. The use of pointers

Guess you like

Origin blog.csdn.net/guanguan12319/article/details/130916955
Recommended