Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer methods on non pointer types

According to this response to this question

The rule about pointers vs. values for receivers is that value methods can be invoked on pointers and values, but pointer methods can only be invoked on pointers

But in fact I can execute pointer method on non pointer values:

package main

import "fmt"

type car struct {
    wheels int
}

func (c *car) fourWheels() {
    c.wheels = 4
}

func main() {

    var c = car{}
    fmt.Println("Wheels:", c.wheels)
    c.fourWheels()
    // Here i can execute pointer method on non pointer value
    fmt.Println("Wheels:", c.wheels)
}

So, what is wrong here? Is this a new feature ? or the response to the question is wrong ?

like image 969
rvillablanca Avatar asked Oct 26 '17 13:10

rvillablanca


People also ask

When to use pointer receiver Go?

There are two reasons to use a pointer receiver. The first is so that the method can modify the value that its receiver points to. The second is to avoid copying the value on each method call. This can be more efficient if the receiver is a large struct, for example.

Is it possible to call methods that are declared with a value receiver using pointer?

you can mix and match methods with value receivers and methods with pointer receivers, and use them with variables containing values and pointers, without worrying about which is which. Both will work, and the syntax is the same.

What types of pointers does Go have?

The pointer type in Go is used to point to a memory address where data is stored. Similar to C/C++, Go uses the * operator to designate a type as a pointer. The following snippet shows several pointers with different underlying types.

What is the size of pointer Golang?

A pointer variable can point to the memory address of any value, and the memory size occupied by the pointer variable is a fixed value, regardless of the size of the value it points to. On a 32-bit machine, the pointer variable occupies 4 bytes, and on a 64-bit machine, it occupies 8 bytes.


1 Answers

You are calling a "pointer method" on a pointer value. In the expression:

c.fourWheels()

c is of type car (non-pointer); since the car.fourWheels() method has a pointer receiver and because the receiver value is a non-pointer and is addressable, it is a shorthand for:

(&c).fourWheels()

This is in Spec: Calls:

If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m().

The statement:

The rule about pointers vs. values for receivers is that value methods can be invoked on pointers and values, but pointer methods can only be invoked on pointers

Interpret it like this:

  • If you have a value method, you can always call it: if you have a value, it's ready to be the receiver; and if you have a pointer, you can always dereference it to obtain a value ready to be the receiver.

  • If you have a pointer method, you may not always be able to call it if you only have a value, as there are several expressions (whose result) are not addressable, and therefore you would not be able to obtain a pointer to it that would be used as the receiver; such examples are function return values and map indexing expressions. For details and examples, see How can I store reference to the result of an operation in Go?; and How to get the pointer of return value from function call? (Sure, you could always assign it to a local variable and take its address, but that's a copy and the pointer method could only modify this copy and not the original.)

like image 195
icza Avatar answered Sep 21 '22 14:09

icza