Reading about value receivers vs pointer receivers across the web and stackoverflow, I understand the basic rule to be: If you don't plan to modify the receiver, and the receiver is relatively small, there is no need for pointers. Currently, methods are not allowed to have an interface receiver or an interface pointer receiver. It is also possible to implement interfaces using pointer receivers. The Print() methods accept a receiver pointer. Hence, the interface must also accept a receiver pointer. Further investigations shows that all of the following changes would solve the problem: But there is a caveat here It is to be noted that the pointer receiver can also be used to implement an interface. Implementing interfaces using pointer receivers vs value receivers. What is a pointer? Declaring pointers; Zero value of a pointer There is a subtlety to be noted while implementing interfaces using pointer receivers. Custom errors in golang and pointer receivers. In this tutorial we will learn how pointers work in Go and we will also understand how Go pointers differ from pointers in other languages such as C and C++. 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. Another subtlety of interfaces is that an interface definition does not prescribe whether an implementor should implement the interface using a pointer receiver or a value receiver. Viewed 4k times 9. There is a caveat while pointer vs value receiver for methods of a type when that type implements an interface . Pointer vs Value Receiver in methods while implementing an interface in Go (Golang) Posted on July 18, 2020 July 18, 2020 by admin. 15 in Golang tutorial series. There are two reasons to use a pointer receiver. Ask Question Asked 2 years, 6 months ago. When traversing the list the recursion base test checking this parent attribute against NIL does not work. Active 1 year, 7 months ago. Welcome to tutorial no. Pointers and interfaces. When you are given an interface value, there’s no guarantee whether the underlying type is or isn’t a pointer. This can be more efficient if the receiver is a large struct, for example. 3. Part 15: Pointers 13 April 2019. Pointer Receiver while implementing an interface. The recursion continues, with calling the method on a NIL pointer receiver. A method of a type can either have a pointer receiver or a value receiver. Interface Accepting Address of the Variable. I'm just building a simple list using an interface type as parent attribute. While methods with interface (value) receivers are clearly not possible, methods with interface pointer receivers (and consequently having interface pointers implement other interfaces) should be possible. If a method accepts a type value, then the interface must receive a type value; if a method has a pointer receiver, then the interface must receive the address of the variable of the respective type. A method of a type can either have a pointer receiver or a value receiver. This tutorial has the following sections. Choosing a value or pointer receiver. All the example interfaces we discussed in part 1 were implemented using value receivers. In the above examples, we only worked with the value receiver.