当前位置:网站首页>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
边栏推荐
- Snowflake won the 2021 annual database
- Ml and NLP are still developing rapidly in 2021. Deepmind scientists recently summarized 15 bright research directions in the past year. Come and see which direction is suitable for your new pit
- I want to talk about yesterday
- Awk getting started to proficient series - awk quick start
- Global and Chinese markets for environmental disinfection robots 2022-2028: Research Report on technology, participants, trends, market size and share
- VBA, JSON interpretation, table structure -json string conversion
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5
- Clockwise rotation method of event arrangement -- PHP implementation
- Map container
- Globalsign's SSL certificate products
猜你喜欢
【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
17.内存分区与分页
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
[the way of programmer training] - 2 Perfect number calculation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution
Introduction to random and threadlocalrandom analysis
记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题
C language function
C语言数组
随机推荐
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
Games101 Lesson 8 shading 2 Notes
[notes] in depth explanation of assets, resources and assetbundles
priority_ queue
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
VBA, JSON interpretation, table structure -json string conversion
In 2022, financial products are not guaranteed?
Global and Chinese market of ice water machines 2022-2028: Research Report on technology, participants, trends, market size and share
[solve the error of this pointing in the applet] SetData of undefined
It's hard to hear C language? Why don't you take a look at this (V) pointer
Iterm tab switching order
Leetcode: 408 sliding window median
Exness: positive I win, negative you lose
Awk getting started to proficient series - awk quick start
queue
Xshell's ssh server rejected the password, failed to skip publickey authentication, and did not register with the server
C fonctions linguistiques
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
. Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12