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.
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 }
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 }
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}] }
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!