The difference between struct pointer and struct value transfer in Golang functions

WBOY
Release: 2024-05-31 19:43:59
Original
886 people have browsed it

In Go, function parameters can be passed by value or pointer. Value passing passes a copy, and modifications to the copy will not affect the original value; pointer passing passes the address, and modifications to the content will be reflected in the original value. In practice, pointer passing can be used to optimize code, for example when sorting slices. Choosing the right delivery method is critical to optimizing your code and improving performance.

Golang 函数中 struct 指针与 struct 值传递的区别

Function parameter passing in Go: value passing and pointer passing

In Go, function parameters can be passed by value or pointer Delivery by delivery. Understanding the difference between these two delivery methods is crucial to mastering Go programming.

Value passing

Value passing means that the function receives a copy of the incoming parameter value. Any modification to the parameter value will not affect the original value. For example:

func changeValue(s string) {
    s = "modified"
}

func main() {
    var s = "original"
    changeValue(s)
    fmt.Println(s) //输出: original
}
Copy after login

In the changeValue function, s is a copy of the string type. Modifications to s will not affect the original s variables in the main function.

Pointer passing

Pointer passing refers to the address of the function receiving the incoming parameters. Any modifications to the parameter content will be reflected in the original variable. For example:

func changePointer(s *string) {
    *s = "modified"
}

func main() {
    var s = "original"
    changePointer(&s)
    fmt.Println(s) //输出: modified
}
Copy after login

In the changePointer function, s is a pointer to the original string variable s. Modifications to *s will directly modify the original s variable in the main function.

Practical case

The following is an example of using pointer passing to optimize code in a real scenario:

type Person struct {
    Name string
    Age  int
}

func sortByName(people []Person) {
    for i := 0; i < len(people)-1; i++ {
        for j := i + 1; j < len(people); j++ {
            if people[i].Name > people[j].Name {
                // 交换 people[i] 和 people[j]
                people[i], people[j] = people[j], people[i]
            }
        }
    }
}

func main() {
    people := []Person{
        {"Alice", 20},
        {"Bob", 25},
        {"Charlie", 30},
    }
    sortByName(people)
    fmt.Println(people) //输出: [{Alice 20} {Bob 25} {Charlie 30}]
}
Copy after login

In this example, ## The #sortByName function receives a pointer to a Person slice. By passing in a pointer, this function can directly modify the contents of the slice without creating a copy. This improves the efficiency of the function, especially when the slices are large.

Conclusion

Understanding the difference between passing by value and passing by pointer is crucial to writing Go programs efficiently. Passing by value is suitable when a copy needs to be modified rather than the original value, while passing by pointer is suitable when the original value needs to be modified. Choosing the correct delivery method for your needs can optimize your code and improve your program's performance.

The above is the detailed content of The difference between struct pointer and struct value transfer in Golang functions. 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!