How do I modify values through pointers in Go functions, and what are the different approaches?

Susan Sarandon
Release: 2024-10-28 05:10:30
Original
559 people have browsed it

How do I modify values through pointers in Go functions, and what are the different approaches?

Changing Values Through Pointers in Go

In Go, passing pointers to functions allows for direct manipulation of the pointed values. However, there are certain nuances to consider when assigning values to pointers.

Assigning Pointers Without Value Modification

Consider the following code:

<code class="go">type Test struct {
    Value int
}

func main() {
    var i Test = Test{2}
    var p *Test = &i
    f(p)
    println(i.Value) // 2
}

func f(p *Test) {
    // Assigning a new pointer value
    p = &Test{4}
}</code>
Copy after login

In this code, p is a pointer to the i struct. Within the f function, assigning a new pointer value to p does not modify the value of i. This is because p is a local variable within f and changing its value only affects the local copy.

Modifying Pointed Values

To actually modify the value of i, you need to change the value of the pointed struct. This can be done through the dereference operator (*). For example:

<code class="go">func f(p *Test) {
    p.Value = 4
}</code>
Copy after login

Here, p.Value dereferences the pointer p and assigns the value 4 to the Value field of the pointed struct. This change will be reflected in i when f returns.

Assigning Pointers to Pointers

Alternatively, you can modify the pointer itself within the f function. However, this requires passing the address of the pointer variable (&p) to f.

<code class="go">func f(p **Test) {
    *p = &Test{4}
}</code>
Copy after login

In this case, *p dereferences the double-pointer p and assigns a new pointer value, effectively changing the p variable in main. However, this approach is not as straightforward and can be less efficient than modifying the pointed value directly.

Conclusion

When passing pointers to functions, it's crucial to understand the difference between assigning new pointers (p = &Test{4}) and modifying the pointed values (p.Value = 4). The latter approach allows for direct manipulation of the struct value, while the former only changes the pointer itself.

The above is the detailed content of How do I modify values through pointers in Go functions, and what are the different approaches?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!