Home > Backend Development > Golang > Why Should I Avoid Using Pointers to Interface Values When Calling Methods in Go?

Why Should I Avoid Using Pointers to Interface Values When Calling Methods in Go?

DDD
Release: 2024-12-07 05:15:16
Original
380 people have browsed it

Why Should I Avoid Using Pointers to Interface Values When Calling Methods in Go?

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
}
Copy after login

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:

  • Pointers to Interfaces: You will never use a pointer to an interface value in Go. Interfaces are abstracted, value types.
  • Call by Value: In Go, all function arguments are passed by value. This includes pointers.
  • Modifying Values: When you pass a pointer to a struct or other type, you can modify the original value. However, this is not the case with pointers to interfaces.

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
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template