


What is the difference between the receiver of a method in golang being a pointer and not being a pointer?
The following column of golang tutorial will give you a detailed explanation of the difference between the receiver of the method in golang. I hope it is a pointer and not a pointer. Help those in need!
Preface
Recently I read a website and a student asked me what is the difference between the receiver of a method in golang being a pointer and not being a pointer. Here I will make it simple and easy to understand. The method is explained 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 the problems mentioned above.
Method is actually a special function, 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) }
Is it easy to understand? ?Now let’s add the code to see what the difference is 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) }
I guess some students have already understood that when the actual parameters are not pointers, the value is copied. So every call A copy of a TestValue() value occurs.
What will be the result if it involves the operation of 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() }
So all students must pay attention when encountering such problems in programming.
So what is the relationship between these method sets? Here I borrow qyuhen’s reading notes in golang (https://github.com/qyuhen/book). 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 What is 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Using predefined time zones in Go includes the following steps: Import the "time" package. Load a specific time zone through the LoadLocation function. Use the loaded time zone in operations such as creating Time objects, parsing time strings, and performing date and time conversions. Compare dates using different time zones to illustrate the application of the predefined time zone feature.

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.
