当前位置:网站首页>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 mapfunc perhaps chan, 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 types sync.Mutex, Or similar synchronous variables ,receiver It has to be a pointer , This avoids copying objects
  • If receiver It's bigger struct perhaps array, 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 perhaps slice, 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 perhaps array, And its variables are invariants 、 Constant , for example time.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

原网站

版权声明
本文为[Red hat spongebob]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141252270708.html