当前位置:网站首页>When to use pointers in go?
When to use pointers in go?
2022-07-04 12:40:00 【Red hat spongebob】
1 Using pointers in methods
What is? receiver?
func (t T) method_name(t T){
}
Inside T Namely receiver
- Use receiver As a method parameter
func main() {
r := receiver{
Name: "zs"}
fmt.Println(r)
r.methodA()
fmt.Println(r)
}
type receiver struct {
Id int
Name string
Age int
}
func (receiver receiver) methodA() {
receiver.Name = "ls"
}
func (receiver *receiver) methodB() {
receiver.Name = "ls"
}
result :
{0 zs 0}
{0 zs 0}
- Use *receiver As a method parameter
func main() {
r := receiver{
Name: "zs"}
fmt.Println(r)
r.methodB()
fmt.Println(r)
}
type receiver struct {
Id int
Name string
Age int
}
func (receiver receiver) methodA() {
receiver.Name = "ls"
}
func (receiver *receiver) methodB() {
receiver.Name = "ls"
}
result :
{0 zs 0}
{0 ls 0}
2 Use pointers in structures
- Mode one
func main() {
student := Student{
Map: map[string]int{
"S": 0}, ReceiverA: receiver{
Name: "A"}, ReceiverB: &receiver{
Name: "B"}}
fmt.Println(student, *student.ReceiverB)
student.updateA()
fmt.Println(student, *student.ReceiverB)
}
type receiver struct {
Id int
Name string
Age int
}
type Student struct {
No int
Map map[string]int
ReceiverA receiver
ReceiverB *receiver
}
func (stu Student) updateA() {
stu.Map["a"] = 1
stu.ReceiverA = receiver{
Name: "ww"}
stu.ReceiverB = &receiver{
Name: "ww"}
}
func (stu *Student) updateB() {
stu.Map["b"] = 2
stu.ReceiverA = receiver{
Name: "ww"}
stu.ReceiverB = &receiver{
Name: "ww"}
}
result :
{0 map[S:0] {0 A 0} 0xc0000b4000} {0 B 0}
{0 map[S:0 a:1] {0 A 0} 0xc0000b4000} {0 B 0}
- Mode two
func main() {
student := Student{
Map: map[string]int{
"S": 0}, ReceiverA: receiver{
Name: "A"}, ReceiverB: &receiver{
Name: "B"}}
fmt.Println(student, *student.ReceiverB)
student.updateA()
fmt.Println(student, *student.ReceiverB)
}
type receiver struct {
Id int
Name string
Age int
}
type Student struct {
No int
Map map[string]int
ReceiverA receiver
ReceiverB *receiver
}
func (stu Student) updateA() {
stu.Map["a"] = 1
stu.ReceiverA = receiver{
Name: "ww"}
stu.ReceiverB = &receiver{
Name: "ww"}
}
func (stu *Student) updateB() {
stu.Map["b"] = 2
stu.ReceiverA = receiver{
Name: "ww"}
stu.ReceiverB = &receiver{
Name: "ww"}
}
result :
{0 map[S:0] {0 A 0} 0xc0000b4000} {0 B 0}
{0 map[S:0 b:2] {0 ww 0} 0xc0000b4060} {0 ww 0}
3 When to use the pointer
When should a function be done with pointer type receiver It has always been a headache for beginners . Here are some common judgment guides .
- If receiver yes
map
、func
perhapschan
, Don't use the pointer - If receiver yes
slice
And this function does not modify this slice, Don't use the pointer - If this function will be modified receiver, At this point, be sure to use the pointer
- If receiver yes
struct
And contains mutually exclusive typessync.Mutex
, Or similar synchronous variables ,receiver It has to be a pointer , This avoids copying objects - If receiver It's bigger
struct
perhapsarray
, Using pointers is more efficient . How old is big ? hypothesis struct All members of the function must be passed in as function variables , If you think there are too many data at this time , Namely struct Too big - If receiver yes
struct
,array
perhapsslice
, And one of them element Points to a variable , At this time receiver Selecting pointers makes the intent of the code more obvious - If receiver Make smaller
struct
perhapsarray
, And its variables are invariants 、 Constant , for exampletime.Time
,value receiver More suitable , because value receiver It can reduce the amount of garbage that needs to be recycled . - Last , If you are not sure which one to use , Using pointer class receiver
Reference article :
https://zhuanlan.zhihu.com/p/395747448
边栏推荐
- Introduction to random and threadlocalrandom analysis
- 2022, 6G is heating up
- 【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
- Uva536 binary tree reconstruction tree recovery
- In 2022, financial products are not guaranteed?
- Article download address
- IIS error, unable to start debugging on the webserver
- Global and Chinese market of cardiac monitoring 2022-2028: Research Report on technology, participants, trends, market size and share
- Iterm tab switching order
- . Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
猜你喜欢
Fastlane one click package / release app - usage record and stepping on pit
[the way of programmer training] - 2 Perfect number calculation
Detailed explanation of NPM installation and caching mechanism
Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution
Show recent errors only command /bin/sh failed with exit code 1
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
When synchronized encounters this thing, there is a big hole, pay attention!
It's hard to hear C language? Why don't you take a look at this (V) pointer
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
随机推荐
C语言:围圈报号排序问题
Hongke case study on storm impact in coastal areas of North Carolina using lidar
Paper notes ACL 2020 improving event detection via open domain trigger knowledge
DDS-YYDS
[Chongqing Guangdong education] National Open University spring 2019 2727 tax basis reference questions
What if the chat record is gone? How to restore wechat chat records on Apple Mobile
Iframe to only show a certain part of the page
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
轻松玩转三子棋
昨天的事情想说一下
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
MYCAT middleware installation and use
Classification and application of AI chips
Haproxy cluster
Introduction to random and threadlocalrandom analysis
BCD code Baidu Encyclopedia
JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup
I want to talk about yesterday
Azure solution: how can third-party tools call azure blob storage to store data?