Calling Methods on Interface Pointers in Go
In Go, you may encounter the need to program against an interface and use transactions without modifying your code. A common approach is to pass a pointer to an "object" into a field property to enable rollbacks if necessary. However, this approach can lead to confusion.
Let's consider a code example:
package repositories import ( "github.com/coopernurse/gorp" ) type Repository struct { Gorp *gorp.SqlExecutor // Pointer to SqlExecutor } func (r *Repository) GetById(i interface{}, key interface{}) interface{} { obj, err := r.Gorp.Get(i, key) // Call method on pointer if err != nil { panic(err) } return obj }
Here, you might believe that you need a pointer to the Gorp "object" to perform rollbacks. However, it's important to understand some key concepts in Go:
In the example code, even though you have a pointer to the Gorp SqlExecutor, you are not able to call methods on the interface pointer itself. You are instead calling methods on the underlying value. In this case, the underlying value is a SqlExecutor struct.
Therefore, you can safely remove the pointer from the Repository struct and simply pass the SqlExecutor interface to the Repository:
package repositories import ( "github.com/coopernurse/gorp" ) type Repository struct { Gorp gorp.SqlExecutor // Pointer removed } func (r *Repository) GetById(i interface{}, key interface{}) interface{} { obj, err := r.Gorp.Get(i, key) if err != nil { panic(err) } return obj }
This code will work as expected and allow you to use transactions without modifying the underlying SqlExecutor. You don't need to worry about passing pointers to interfaces in this context.
The above is the detailed content of Why Should I Avoid Using Pointers to Interface Values When Calling Methods in Go?. For more information, please follow other related articles on the PHP Chinese website!