Go Receiver Methods Calling Syntax Confusion
In the "Pointers vs. Values" section of Effective Go, it states that "value methods can be invoked on pointers and values, but pointer methods can only be invoked on pointers." This is due to the fact that pointer methods can modify the receiver, and invoking them on a copy of the value would cause these modifications to be discarded.
However, some developers have encountered a situation where they were able to invoke a pointer method on a value, which seemingly contradicts the documented syntax rules. To clarify this confusion, we will explore this behavior in more detail.
Consider the following Go code:
<code class="go">package main import ( "fmt" "reflect" ) type age int func (a age) String() string { return fmt.Sprintf("%d yeasr(s) old", int(a)) } func (a *age) Set(newAge int) { if newAge >= 0 { *a = age(newAge) } } func main() { var vAge age = 5 pAge := new(age) fmt.Printf("TypeOf =>\n\tvAge: %v\n\tpAge: %v\n", reflect.TypeOf(vAge), reflect.TypeOf(pAge)) fmt.Printf("vAge.String(): %v\n", vAge.String()) fmt.Printf("vAge.Set(10)\n") vAge.Set(10) fmt.Printf("vAge.String(): %v\n", vAge.String()) fmt.Printf("pAge.String(): %v\n", pAge.String()) fmt.Printf("pAge.Set(10)\n") pAge.Set(10) fmt.Printf("pAge.String(): %v\n", pAge.String()) }</code>
In this example, we define a age type with two methods: String() (value method) and Set() (pointer method). The code creates two variables: vAge, a value of type age, and pAge, a pointer to an age value.
Despite the documentation indicating that vAge.Set() should not be valid syntax, the code compiles without errors. This is because vAge is addressable, meaning it has a memory address that can be referenced. As per the language specification, a method call x.m() on an addressable variable x is valid if x's method set contains m.
In this case, vAge is addressable, and since the method set of *age contains Set(), the call vAge.Set() is shorthand notation for (&vAge).Set(). Essentially, the & operator is being used implicitly to obtain the pointer to vAge.
Therefore, the code demonstrates that value methods can indeed be invoked on pointers because invoking them on an addressable value is equivalent to calling the method on a pointer to that value. The pointer method Set() is still only invocable on pointers, but in the case where the value is addressable, the implicit dereferencing allows for the syntax vAge.Set() to be valid.
The above is the detailed content of ## Why Can I Call a Pointer Method on a Value in Go?. For more information, please follow other related articles on the PHP Chinese website!

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

The article discusses Go's reflect package, used for runtime manipulation of code, beneficial for serialization, generic programming, and more. It warns of performance costs like slower execution and higher memory use, advising judicious use and best

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization

The article discusses using table-driven tests in Go, a method that uses a table of test cases to test functions with multiple inputs and outcomes. It highlights benefits like improved readability, reduced duplication, scalability, consistency, and a

The article discusses managing Go module dependencies via go.mod, covering specification, updates, and conflict resolution. It emphasizes best practices like semantic versioning and regular updates.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
