


Value vs. Pointer Receivers in Go Interfaces: When Does Interface Assignment Fail?
Understanding Go Structure Methods and Interface Implementation
In Go, methods that fulfill an interface can be categorized into two types: methods with value receivers and methods with pointer receivers. When implementing interfaces, however, the assignability of values to the interface differs from direct struct method calls.
Consider the provided code snippet:
type greeter interface { hello() goodbye() }
The greeter interface defines two methods, hello and goodbye.
type tourGuide struct { name string }
The tourGuide struct implements the greeter interface.
func (t tourGuide) hello() { fmt.Println("Hello", t.name) }
Method hello has a value receiver, which allows for direct method invocation using a variable of type tourGuide.
func (t *tourGuide) goodbye() { fmt.Println("Goodbye", t.name) }
Method goodbye, on the other hand, has a pointer receiver, which requires a pointer variable to invoke.
Now, let's examine the interface implementation:
var g2 greeter = t2 g2.hello() // Hello Smith g2.goodbye() // Goodbye Smith
Assigning a pointer variable t2 of type *tourGuide to a receiver value of interface type greeter succeeds because the pointer receiver allows for the acquisition of the value's address, which is then used as the receiver.
var g1 greeter = t1
However, assigning a non-pointer variable t1 of type tourGuide to a receiver value of interface type greeter fails. This is because a method with a pointer receiver requires a pointer receiver, and a value itself cannot be directly used as a pointer.
To summarize, methods with value receivers can be invoked using either a value or a pointer, while methods with pointer receivers can only be invoked using a pointer. When implementing interfaces with methods that have pointer receivers, it's crucial to use pointers as the underlying type.
The above is the detailed content of Value vs. Pointer Receivers in Go Interfaces: When Does Interface Assignment Fail?. 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

AI Hentai Generator
Generate AI Hentai for free.

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

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework?

How to convert MySQL query result List into a custom structure slice in Go language?

How can I define custom type constraints for generics in Go?

How do I write mock objects and stubs for testing in Go?

How to write files in Go language conveniently?

How can I use tracing tools to understand the execution flow of my Go applications?
