Home > Backend Development > Golang > How to Modify Values Through Pointer Receiver Methods in Go?

How to Modify Values Through Pointer Receiver Methods in Go?

Mary-Kate Olsen
Release: 2024-11-14 21:52:02
Original
1023 people have browsed it

How to Modify Values Through Pointer Receiver Methods in Go?

Pointer Receiver Methods and Value Modification in Go

In Go, a pointer receiver function allows you to modify the value of the receiver object. However, understanding how pointers work in Go is crucial for successful implementation.

Issue Resolution

When trying to modify the value of a simple type through a pointer receiver method, one might encounter situations where the changes do not persist outside the method. This is because all method arguments, including the receiver, are copied locally within the method's execution.

Method Argument Copy

In the example provided:

func (fi *FooInt) FromString(i string) {
    num, _ := strconv.Atoi(i)
    tmp := FooInt(num)
    fi = &tmp
}
Copy after login

The fi pointer argument is a copy of the original fi pointer in main. Therefore, changes made to the copied fi pointer within the FromString method only affect the local copy, not the original pointer.

Solution

To resolve this, there are a few options:

  1. Return the Updated Pointer:

Create a return statement that assigns the updated pointer to the receiver, and then reassign the returned pointer in main.

// Return the updated pointer and reassign it in main
func (fi *FooInt) FromString(i string) *FooInt {
    num, _ := strconv.Atoi(i)
    tmp := FooInt(num)
    return &tmp
}

// Reassign the updated pointer in main
func main() {
    var fi *FooInt
    fi = fi.FromString("5")
    fmt.Printf("%v %v\n", fi, *fi) // Outputs: 0xc0000b4020 5
}
Copy after login
  1. Pass a Non-nil Pointer:

Pass a non-nil pointer of the target type as an argument to the method.

// Pass a non-nil pointer as an argument
func (fi *FooInt) FromString(i string, p **FooInt) {
    num, _ := strconv.Atoi(i)
    tmp := FooInt(num)
    *p = &tmp
}

// Create a non-nil pointer and pass it to the method in main
func main() {
    var fi *FooInt
    fi.FromString("5", &fi)
    fmt.Printf("%v %v\n", fi, *fi) // Outputs: 0xc0000b4020 5
}
Copy after login
  1. Ensure Non-Nil Receiver:

Check if the receiver pointer is non-nil before modifying it.

// Check if the receiver is non-nil before modifying
func (fi *FooInt) FromString(i string) {
    if fi == nil {
        return
    }
    num, _ := strconv.Atoi(i)
    *fi = FooInt(num)
}

// Create a non-nil receiver in main
func main() {
    fi := new(FooInt)
    fi.FromString("5")
    fmt.Printf("%v %v\n", fi, *fi) // Outputs: 0xc0000b4020 5
}
Copy after login

The above is the detailed content of How to Modify Values Through Pointer Receiver 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template