Home > Backend Development > Golang > The difference between the receiver of a method in golang being a pointer and not being a pointer

The difference between the receiver of a method in golang being a pointer and not being a pointer

藏色散人
Release: 2021-05-14 18:04:45
forward
2159 people have browsed it

The following tutorial column will introduce to you the difference between the receiver of a method in golang being a pointer and not being a pointer. I hope it will be helpful to friends in need! The difference between the receiver of a method in golang being a pointer and not being a pointer

Preface

Recently, I read a website where a classmate asked whether the receiver of a method in golang is a pointer or not. What is the difference between pointers? Here I will explain it in a simple and easy-to-understand method to help students who have just learned golang.

What is the method

In fact, as long as you understand this principle, you can basically understand it. The problem mentioned above.

The method is actually a special function, and the receiver is the first parameter passed in implicitly.

For example

type test struct{
    name string
}

func (t test) TestValue() {
}

func (t *test) TestPointer() {
}

func main(){
    t := test{}
    
    m := test.TestValue
    m(t)
    
    m1 := (*test).TestPointer
    m1(&t)    
}
Copy after login
Is it easy to understand? Now let’s add the code to see the difference between pointers and non-pointers.
type test struct{
    name string
}

func (t test) TestValue() {
    fmt.Printf("%p\n", &t)
}

func (t *test) TestPointer() {
    fmt.Printf("%p\n", t)
}

func main(){
    t := test{}
    //0xc42000e2c0
    fmt.Printf("%p\n", &t)
    
    //0xc42000e2e0
    m := test.TestValue
    m(t)
    
    //0xc42000e2c0
    m1 := (*test).TestPointer
    m1(&t)    

}
Copy after login

I guess some students have already understood that when it is not a pointer After the actual parameters are passed in, the value is copied. So every time TestValue() is called, the value is copied.

So what will be the result if it involves modifying the value?

type test struct{
    name string
}

func (t test) TestValue() {
    fmt.Printf("%s\n",t.name)
}

func (t *test) TestPointer() {
    fmt.Printf("%s\n",t.name)
}

func main(){
    t := test{"wang"}

    //这里发生了复制,不受后面修改的影响
    m := t.TestValue
    
    t.name = "Li"
    m1 := (*test).TestPointer
    //Li
    m1(&t)    
    
    //wang
    m()
}
Copy after login
So everyone Students must pay attention when encountering such problems in programming.

So what is the relationship between these method sets? Here I borrowed qyuhen’s notes from studying in golang. I also recommend friends who like golang to read this book. , which is very helpful for deepening the understanding of golang.

• Type T method set contains all receiver T methods.
• The type

T method set contains all receiver T
T methods.

• If type S contains anonymous field T, then S method set contains T method.
• If type S contains anonymous field T, then S's method set contains T T methods.
• Regardless of embedding T or
T, the set of S methods always contains T *T methods.
Conclusion

Although golang is simple and easy to use, it still has many pitfalls. The author encountered many pitfalls in the process of using golang, which will be mentioned in the blog later. Everyone is welcome. discuss together.

The above is the detailed content of The difference between the receiver of a method in golang being a pointer and not being a pointer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template