golang error: 'invalid pointer receiver...' How to solve it?

PHPz
Release: 2023-06-25 10:33:05
Original
892 people have browsed it

When using golang to develop projects, you may occasionally encounter some unexpected errors. One of them is the "invalid pointer receiver..." error, which usually breaks the structure in the code and causes the program to crash. In this article, I will explain the cause of this error and provide some possible solutions.

1. Error reason

Pointers in Golang are very powerful, but they must be used with great care. If a pointer receiver is defined in a method and a non-pointer value is passed when the method is called, an "invalid pointer receiver..." error will occur. For example:

type Book struct {
  Name     string
  Author   string
  Pages    int
  Progress float64
}

func (b *Book) Read(pageCount int) {
  b.Progress = float64(pageCount)/float64(b.Pages) * 100
}

func main() {
  myBook := Book{
    Name:     "Golang book",
    Author:   "John Doe",
    Pages:    200,
    Progress: 0,
  }

  myBook.Read(50) // invalid pointer receiver Book literal
}
Copy after login

In this example, the Read method is defined as a pointer receiver, meaning it requires a pointer to a Book to work. However, we are passing only a Book literal while calling the Read method, hence the "invalid pointer receiver Book literal" error.

2. Solution

In order to solve the error, we need to ensure that the pointer value is passed when calling the Read method. There are several ways to achieve this.

2.1 Pass the structure variable address to the method

This is the most common solution. Just pass it the address of the structure variable when calling the method. For example:

myBook := &Book{
  Name:     "Golang book",
  Author:   "John Doe",
  Pages:    200,
  Progress: 0,
}

myBook.Read(50) // no error
Copy after login

In this example, we only need to add the "&" symbol before the structure variable to get its address. This address can be passed to the Read method since the method requires a pointer to the Book.

2.2 Change method receiver

If we really want to pass the literal of the structure to the method, then we can try to change the pointer receiver in the method definition to a non-pointer receiver . For example:

func (b Book) Read(pageCount int) {
  b.Progress = float64(pageCount)/float64(b.Pages) * 100
}

func main() {
  myBook := Book{
    Name:     "Golang book",
    Author:   "John Doe",
    Pages:    200,
    Progress: 0,
  }

  myBook.Read(50) // no error
}
Copy after login

In this example, we change the receiver of the Read method from "*Book" to "Book", which means that it expects a Book value instead of a pointer. Now we can pass a Book literal to it without error.

3. Summary

The "invalid pointer receiver..." error is usually caused by incorrectly specifying the pointer receiver. When writing any program, we should handle pointers carefully to ensure the correctness and reliability of the code. In this article we explore two possible solutions, passing the address and changing the receiver type. Hope this article helps you solve similar errors.

The above is the detailed content of golang error: 'invalid pointer receiver...' How to solve it?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!