当前位置:网站首页>Golang learning notes - pointer
Golang learning notes - pointer
2022-06-21 21:04:00 【Sentiment.】
C I have never learned the pointer of , Happen to happen Golang There is a by-pass look inside .
The pointer
Go The function parameters in the language are all value copies , When we want to modify a variable , We can create a pointer to the address of the variable . Passing data using pointers , Without copying data .
The difference in C The pointer to ,Golang The middle pointer cannot be offset and operated
Two symbols :&( Address fetch )、*( According to the address )
grammar
A memory address that points to a value .
Similar to variables and constants , You need to declare a pointer before you use it . Format :
var var_name *var_type
Demo01
package main
import "fmt"
func main() {
var p *int
fmt.Printf("p:%v\n", p) // Default to when not assigned nil
fmt.Printf("p:%T\n", p) // The type is *init
a := 100
p = &a //& take a The address of
fmt.Printf("%v\n", p) //p Direct output address
fmt.Printf("%v", *p) //*p Output the value corresponding to the address
}
result :
p:<nil>
p:*int
0xc00000a0c0
100
Array pointer
grammar
var var_name [MAX]*int;
Demo02
package main
import "fmt"
func main() {
a := [3]int{
1, 2, 3}
var p [3]*int
fmt.Printf("p:%v\n", p) // Default to when not assigned nil
for i := 0; i < len(a); i++ {
p[i] = &a[i]
}
fmt.Printf("p:%v\n", p) // After assignment, the address is output
for i := 0; i < len(p); i++ {
fmt.Printf("%v ", *p[i]) // Get the value of the corresponding address
}
}
result :
p:[<nil> <nil> <nil>]
p:[0xc000014150 0xc000014158 0xc000014160]
1 2 3
You can also calculate the address through the pointer
package main
import (
"fmt"
"unsafe"
)
func main() {
p := [3]int8{
1, 2, 3}
//1. Get the address pointer of the first element of the array
var f = &p[0]
//2. convert to Pointer type
ptr := unsafe.Pointer(f)
//3. convert to uintptr type , Then calculate the memory address ( Address plus a byte , It means taking the value of the second index )
targetAddress := uintptr(ptr) + 1
//4. According to the new address , Convert it back to Pointer type
newPtr := unsafe.Pointer(targetAddress)
//5.Pointer Object conversion to int8 Pointer types
value := (*int8)(newPtr)
fmt.Println(*value)
}
边栏推荐
- Details, MySQL_ DATE_ FORMAT()_ Functions_ Detailed explanation (remember to collect)
- Random forest learning notes
- Servlet usage
- 延长 洁鲜生密实袋,给食材上一把“安全锁”
- 行业首家!极氪APP获中国网络安全审查技术与认证中心权威认证
- What are the applications of 4.3-inch touch screen intelligent network central control host
- 纵横网络靶场社区-Modbus协议
- Decision tree learning notes
- MySQl学习(从入门到精通 1.2)
- Alibaba cloud ack one and ACK cloud native AI suite have been newly released to meet the needs of the end of the computing era
猜你喜欢
随机推荐
Book list given by Wu Jun to college students
Pfsense configuring tinc site to site tunneling tutorial
欢迎使用Markdown编辑器
LeeCode198 打家劫舍
产品创新 | 物极必反,回归真实生活的创新社交APP
运维监控数据可视化-让数据自己会说话[华汇数据]
In May, I just came back from the Ali software testing post. I worked for Alibaba P7 at 3+1, with an annual salary of 28*15
Check information on the Internet after the college entrance examination, and pay attention to prevent websites without SSL certificates
Rongyun obtains token
Quartus II 18.0软件安装包和安装教程
volatile
Flutter TabBarView组件
Laravel imports and exports excel using phpoffice
Flutter PageView组件
Unity analog flashlight light source detector, AI attack range detection area, object detection in visual cone, fan-shaped area detection, circular area detection, cone area detection
Kubernetes-23:详解如何将CPU Manager做到游刃有余
SQL 面试题:2022 年最热门的 15 个问题
Most detailed collation of vector basis of STL
Jingdong 39 year old "graduate" found a new job within a week after being laid off, with a salary increase of 20%!
Extend the clean, fresh and dense bag, and put a "safety lock" on the ingredients









