Array parameter application method of Golang function
Golang is an open source programming language that is easy to learn, efficient and safe, and has garbage collection features. Arrays are a very basic data type in Golang. Arrays are also a common way to write programs as function parameters. This article will introduce the application method of arrays as function parameters in Golang, hoping to inspire readers in the application of Golang functions.
When an array is passed as a function value, the formal parameter of the function will get a copy equal to the actual parameter value. Modifications to formal parameters do not affect actual parameters. This method is suitable for situations where you need to perform some simple operations on arrays in a function.
For example, the following code:
package main import "fmt" func modifyArr(arr [3]int) { for i := 0; i < len(arr); i++ { arr[i] += i } } func main() { var arr [3]int = [3]int{1, 2, 3} fmt.Printf("Before modified, arr = %v ", arr) modifyArr(arr) fmt.Printf("After modified, arr = %v ", arr) }
The output result is as follows:
Before modified, arr = [1 2 3] After modified, arr = [1 2 3]
It can be found that even if the array is modified in the modifyArr function, the array is printed in the main function value, the value of the array has not changed.
When the array is used as a pointer to a function to pass parameters, the function can directly modify the actual parameter group. This method is suitable for situations where Situations where more complex operations are performed in functions.
The following is a code example:
package main import "fmt" func modifyArrByPointer(arr *[3]int) { for i := 0; i < len(arr); i++ { arr[i] += i } } func main() { var arr [3]int = [3]int{1, 2, 3} fmt.Printf("Before modified, arr = %v ", arr) modifyArrByPointer(&arr) fmt.Printf("After modified, arr = %v ", arr) }
The output results are as follows:
Before modified, arr = [1 2 3] After modified, arr = [1 3 5]
As you can see, in the modifyArrByPointer function, we modify the array through pointer operations.
When passing an array as a slice parameter of a function, all slicing operations of the array can be used inside the function. The advantage of this method is that it will not Copies the entire array, instead passing only the reference. This method is suitable for scenarios where slicing is required to operate on arrays.
For example, the following code:
package main import "fmt" func modifyArrBySlice(arr []int) { for i := 0; i < len(arr); i++ { arr[i] += i } } func main() { var arr [3]int = [3]int{1, 2, 3} fmt.Printf("Before modified, arr = %v ", arr) modifyArrBySlice(arr[:]) fmt.Printf("After modified, arr = %v ", arr) }
The output result is as follows:
Before modified, arr = [1 2 3] After modified, arr = [1 3 5]
As you can see, in the parameters of the modifyArrBySlice function, we use arr[:] to pass the array Slicing, in this way, the array can be sliced.
In Golang, the structure can also contain fields of array type. When passing a structure in a function, you can choose to pass a reference or value of the structure to operate on the array.
For example, the following code:
package main import "fmt" type ArrStruct struct { arr [3]int } func modifyArrByStruct(s ArrStruct) { for i := 0; i < len(s.arr); i++ { s.arr[i] += i } } func modifyArrByStructPointer(s *ArrStruct) { for i := 0; i < len(s.arr); i++ { s.arr[i] += i } } func main() { var s ArrStruct = ArrStruct{[3]int{1, 2, 3}} fmt.Printf("Before modified, arr = %v ", s.arr) modifyArrByStruct(s) fmt.Printf("After modified by value, arr = %v ", s.arr) modifyArrByStructPointer(&s) fmt.Printf("After modified by pointer, arr = %v ", s.arr) }
The output result is as follows:
Before modified, arr = [1 2 3] After modified by value, arr = [1 2 3] After modified by pointer, arr = [1 3 5]
As you can see, when we pass the structure as the value of the function, the function cannot be modified. The value of its member variables, but when the structure is passed as a pointer to a function, its member variables can be modified.
Summary:
In Golang, there are three ways to use arrays as parameters of functions: value passing, pointer passing and slice passing. Value passing is suitable for simple operation scenarios, pointer passing is suitable for scenarios that require complex operations inside functions, and slice passing is suitable for scenarios that require slicing operations on arrays. In addition, in the structure, if the array is a member variable of the structure, you can choose to use the pointer of the structure to pass parameters for modification. In actual applications, developers should choose the appropriate parameter transfer method according to specific needs.
The above is the detailed content of Array parameter application method of Golang function. For more information, please follow other related articles on the PHP Chinese website!