当前位置:网站首页>Introduction to go language pointer
Introduction to go language pointer
2022-06-30 07:03:00 【Duanxiaoman】
Preface
And Java and .NET Different programming languages ,Go Language provides programmers with the ability to control data structure pointers , however , Pointer operations are not possible .Go Language allows you to control the data structure of a specific set 、 Number of allocations and memory access mode , This is very important for building a well functioning system . The impact of pointers on performance is self-evident , If you want to do system programming 、 Operating system or network application , Pointers are an integral part .
The pointer (pointer) stay Go Language can be divided into two core concepts :
- Type a pointer , Allows you to modify the data of this pointer type , You can pass data directly using pointers , Without copying data , Type pointers cannot be offset and operated .
- section , By the original pointer to the starting element 、 Element number and capacity composition .
Benefit from such constraints and splits ,Go The pointer type variable of language has the characteristics of efficient pointer access , There will be no pointer offset , Thus, the problem of illegally modifying key data is avoided . meanwhile , Garbage collection is also easier to retrieve and recycle pointers that will not be offset . Slicing has more powerful features than raw pointers , And safer . When the slice crosses the boundary , Downtime will be reported at runtime , And hit the stack , And the original pointer just crashes .
Know pointer address and pointer type
A pointer variable can point to the memory address of any value , The memory address of the value it points to is 32 and 64 Respectively occupied on bit machines 4 or 8 Bytes , The size of the occupied bytes is independent of the size of the value pointed to . When a pointer is defined and not assigned to any variable , Its default value is nil. Pointer variables are usually abbreviated to ptr.
return nil Indicates that null is returned , And JAVA Inside return null The way of writing is different , But the meaning is the same , So it feels awkward , In fact, although they all mean empty , however Go there nil Indicates a pointer .
Each variable has an address at run time , This address represents the location of the variable in memory .Go Used in languages before variable names add to & The operator ( Prefix ) To get the memory address of the variable ( Address operation ), The format is as follows :
ptr := &v // v The type of T
among v The variable representing the address to be taken , Variable v Address usage variable for ptr Receive ,ptr The type of *T, It's called T Pointer type of ,* For the pointer .
Pointer actual usage , You can learn from the following example :
Two variables are declared in the code , And used fmt.Printf It's a simple verb %p Print cat and str Memory address of variable , The value of the pointer is with 0x A set of data with a hexadecimal prefix .
Tips : Variable 、 The relationship between pointer and address is : Every variable has an address , The value of the pointer is the address .
Get the value that the pointer points to from the pointer
When using & Operator to get the address of a common variable and get the pointer of the variable , You can use * The operator , That is, the value of the pointer , The code is as follows :
Take the address operator & And value operators * It's a pair of complementary operators ,& Take out the address ,* Take the value pointed to by the address .
Variable 、 Pointer address 、 Pointer to the variable 、 Address fetch 、 The relationships and characteristics of values are as follows :
- To get the address of a variable, use & The operator , You can get the pointer variable of this variable .
- The value of the pointer variable is the pointer address .
- The pointer variable can be accessed using * The operator , You can get the value of the original variable that the pointer variable points to .
Use the pointer to modify the value
You can not only take values through pointers , You can also modify the value .
* Operator as the right value , The meaning is to take the value of the pointer , As an lvalue , That is, on the left side of the assignment operator , Express a The variable that the pointer points to . In fact, to sum up ,* The fundamental meaning of an operator is to manipulate the variable that the pointer points to . When the operation is on the right , Is to take the value of the variable , When the operation is on the left , Is to set the value to the variable that you point to .
Another way to create a pointer ——new() function
Go The language also provides another way to create pointer variables , The format is as follows :
new( type )
It's usually written like this :
str := new(string)
*str = "Go Language course "
fmt.Println(*str)
new() Function to create a pointer to the corresponding type , The creation process allocates memory , The created pointer points to the default value .
边栏推荐
- First line of code (Third Edition) learning notes
- 【最全】linux服务器上安装Mysql
- Out of class implementation of member function of class template
- Go语言指针介绍
- sscanf 函数的使用
- Essence of signal slot macros signal and slot
- Google Earth Engine(GEE)——墨累全球潮汐湿地变化 v1 (1999-2019) 数据集
- RT thread migration to s5p4418 (IV): thread synchronization
- 随机网络,无标度网络,小世界网络以及NS小世界的性能对比matlab仿真
- Servlet principle
猜你喜欢

app闪退

Deploying web projects using idea

RT thread migration to s5p4418 (I): scheduler

RT thread migration to s5p4418 (IV): thread synchronization
![[daily question] 535 Encryption and decryption of tinyurl](/img/73/35674cd5df3d937af0762645041421.png)
[daily question] 535 Encryption and decryption of tinyurl

oracle数据库报列表中最大表达式为1000错误

It turns out that you are such an array. You have finally learned
![[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network](/img/39/c9bf235828b6d50e7931876fa4a918.png)
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network

Class template case - encapsulation of array classes

SOC项目AHB_SD_HOST控制器设计
随机推荐
Egret P2 physical engine (1) small ball falling demo
Go installation and configuration (1)
Finished product upgrade procedure
0 basic job transfer software test, how to achieve a monthly salary of 9.5k+
Redis cache
Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
RT thread Kernel Implementation (III): implementation of idle threads and blocking delay
Qstring to const char*
[transfer] analysis of memory structure, cache and DMA architecture
Solr search
Idea shortcut key
B站首个UP主付费观看视频还是来了!价格“劝退”网友
JS widget wave JS implementation of wave progress bar animation style
Record one time of Tencent Test Development Engineer's automation interface test practice experience
明天!“移动云杯”大赛空宣会开播!
[docsify basic use]
RT thread Kernel Implementation (VI): time slice
【Hot100】11. Container with the most water
Qdebug small details
社招两年半10个公司28轮面试面经